2019-05-27 14:55:01 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-17 06:20:36 +08:00
|
|
|
/* align.c - handle alignment exceptions for the Power PC.
|
|
|
|
*
|
|
|
|
* Copyright (c) 1996 Paul Mackerras <paulus@cs.anu.edu.au>
|
|
|
|
* Copyright (c) 1998-1999 TiVo, Inc.
|
|
|
|
* PowerPC 403GCX modifications.
|
|
|
|
* Copyright (c) 1999 Grant Erickson <grant@lcse.umn.edu>
|
|
|
|
* PowerPC 403GCX/405GP modifications.
|
|
|
|
* Copyright (c) 2001-2002 PPC64 team, IBM Corp
|
|
|
|
* 64-bit and Power4 support
|
2005-11-18 11:09:41 +08:00
|
|
|
* Copyright (c) 2005 Benjamin Herrenschmidt, IBM Corp
|
|
|
|
* <benh@kernel.crashing.org>
|
|
|
|
* Merge ppc32 and ppc64 implementations
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <asm/processor.h>
|
2016-12-25 03:46:01 +08:00
|
|
|
#include <linux/uaccess.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <asm/cache.h>
|
|
|
|
#include <asm/cputable.h>
|
2009-05-18 10:10:05 +08:00
|
|
|
#include <asm/emulated_ops.h>
|
2012-03-29 01:30:02 +08:00
|
|
|
#include <asm/switch_to.h>
|
2014-05-12 19:34:06 +08:00
|
|
|
#include <asm/disassemble.h>
|
2016-07-23 17:12:40 +08:00
|
|
|
#include <asm/cpu_has_feature.h>
|
2017-08-30 12:12:40 +08:00
|
|
|
#include <asm/sstep.h>
|
2020-05-06 11:40:26 +08:00
|
|
|
#include <asm/inst.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
struct aligninfo {
|
|
|
|
unsigned char len;
|
|
|
|
unsigned char flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define INVALID { 0, 0 }
|
|
|
|
|
2006-06-07 14:14:40 +08:00
|
|
|
/* Bits in the flags field */
|
|
|
|
#define LD 0 /* load */
|
|
|
|
#define ST 1 /* store */
|
2007-08-10 12:07:38 +08:00
|
|
|
#define SE 2 /* sign-extend value, or FP ld/st as word */
|
2006-06-07 14:14:40 +08:00
|
|
|
#define SW 0x20 /* byte swap */
|
2007-08-25 05:42:53 +08:00
|
|
|
#define E4 0x40 /* SPE endianness is word */
|
|
|
|
#define E8 0x80 /* SPE endianness is double word */
|
2014-03-28 14:01:23 +08:00
|
|
|
|
2007-08-25 05:42:53 +08:00
|
|
|
#ifdef CONFIG_SPE
|
|
|
|
|
|
|
|
static struct aligninfo spe_aligninfo[32] = {
|
|
|
|
{ 8, LD+E8 }, /* 0 00 00: evldd[x] */
|
|
|
|
{ 8, LD+E4 }, /* 0 00 01: evldw[x] */
|
|
|
|
{ 8, LD }, /* 0 00 10: evldh[x] */
|
|
|
|
INVALID, /* 0 00 11 */
|
|
|
|
{ 2, LD }, /* 0 01 00: evlhhesplat[x] */
|
|
|
|
INVALID, /* 0 01 01 */
|
|
|
|
{ 2, LD }, /* 0 01 10: evlhhousplat[x] */
|
|
|
|
{ 2, LD+SE }, /* 0 01 11: evlhhossplat[x] */
|
|
|
|
{ 4, LD }, /* 0 10 00: evlwhe[x] */
|
|
|
|
INVALID, /* 0 10 01 */
|
|
|
|
{ 4, LD }, /* 0 10 10: evlwhou[x] */
|
|
|
|
{ 4, LD+SE }, /* 0 10 11: evlwhos[x] */
|
|
|
|
{ 4, LD+E4 }, /* 0 11 00: evlwwsplat[x] */
|
|
|
|
INVALID, /* 0 11 01 */
|
|
|
|
{ 4, LD }, /* 0 11 10: evlwhsplat[x] */
|
|
|
|
INVALID, /* 0 11 11 */
|
|
|
|
|
|
|
|
{ 8, ST+E8 }, /* 1 00 00: evstdd[x] */
|
|
|
|
{ 8, ST+E4 }, /* 1 00 01: evstdw[x] */
|
|
|
|
{ 8, ST }, /* 1 00 10: evstdh[x] */
|
|
|
|
INVALID, /* 1 00 11 */
|
|
|
|
INVALID, /* 1 01 00 */
|
|
|
|
INVALID, /* 1 01 01 */
|
|
|
|
INVALID, /* 1 01 10 */
|
|
|
|
INVALID, /* 1 01 11 */
|
|
|
|
{ 4, ST }, /* 1 10 00: evstwhe[x] */
|
|
|
|
INVALID, /* 1 10 01 */
|
|
|
|
{ 4, ST }, /* 1 10 10: evstwho[x] */
|
|
|
|
INVALID, /* 1 10 11 */
|
|
|
|
{ 4, ST+E4 }, /* 1 11 00: evstwwe[x] */
|
|
|
|
INVALID, /* 1 11 01 */
|
|
|
|
{ 4, ST+E4 }, /* 1 11 10: evstwwo[x] */
|
|
|
|
INVALID, /* 1 11 11 */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define EVLDD 0x00
|
|
|
|
#define EVLDW 0x01
|
|
|
|
#define EVLDH 0x02
|
|
|
|
#define EVLHHESPLAT 0x04
|
|
|
|
#define EVLHHOUSPLAT 0x06
|
|
|
|
#define EVLHHOSSPLAT 0x07
|
|
|
|
#define EVLWHE 0x08
|
|
|
|
#define EVLWHOU 0x0A
|
|
|
|
#define EVLWHOS 0x0B
|
|
|
|
#define EVLWWSPLAT 0x0C
|
|
|
|
#define EVLWHSPLAT 0x0E
|
|
|
|
#define EVSTDD 0x10
|
|
|
|
#define EVSTDW 0x11
|
|
|
|
#define EVSTDH 0x12
|
|
|
|
#define EVSTWHE 0x18
|
|
|
|
#define EVSTWHO 0x1A
|
|
|
|
#define EVSTWWE 0x1C
|
|
|
|
#define EVSTWWO 0x1E
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Emulate SPE loads and stores.
|
|
|
|
* Only Book-E has these instructions, and it does true little-endian,
|
|
|
|
* so we don't need the address swizzling.
|
|
|
|
*/
|
|
|
|
static int emulate_spe(struct pt_regs *regs, unsigned int reg,
|
2020-05-06 11:40:31 +08:00
|
|
|
struct ppc_inst ppc_instr)
|
2007-08-25 05:42:53 +08:00
|
|
|
{
|
2013-09-23 10:04:46 +08:00
|
|
|
int ret;
|
2007-08-25 05:42:53 +08:00
|
|
|
union {
|
|
|
|
u64 ll;
|
|
|
|
u32 w[2];
|
|
|
|
u16 h[4];
|
|
|
|
u8 v[8];
|
|
|
|
} data, temp;
|
|
|
|
unsigned char __user *p, *addr;
|
|
|
|
unsigned long *evr = ¤t->thread.evr[reg];
|
2020-05-06 11:40:31 +08:00
|
|
|
unsigned int nb, flags, instr;
|
2007-08-25 05:42:53 +08:00
|
|
|
|
2020-05-06 11:40:31 +08:00
|
|
|
instr = ppc_inst_val(ppc_instr);
|
2007-08-25 05:42:53 +08:00
|
|
|
instr = (instr >> 1) & 0x1f;
|
|
|
|
|
|
|
|
/* DAR has the operand effective address */
|
|
|
|
addr = (unsigned char __user *)regs->dar;
|
|
|
|
|
|
|
|
nb = spe_aligninfo[instr].len;
|
|
|
|
flags = spe_aligninfo[instr].flags;
|
|
|
|
|
|
|
|
/* Verify the address of the operand */
|
|
|
|
if (unlikely(user_mode(regs) &&
|
Remove 'type' argument from access_ok() function
Nobody has actually used the type (VERIFY_READ vs VERIFY_WRITE) argument
of the user address range verification function since we got rid of the
old racy i386-only code to walk page tables by hand.
It existed because the original 80386 would not honor the write protect
bit when in kernel mode, so you had to do COW by hand before doing any
user access. But we haven't supported that in a long time, and these
days the 'type' argument is a purely historical artifact.
A discussion about extending 'user_access_begin()' to do the range
checking resulted this patch, because there is no way we're going to
move the old VERIFY_xyz interface to that model. And it's best done at
the end of the merge window when I've done most of my merges, so let's
just get this done once and for all.
This patch was mostly done with a sed-script, with manual fix-ups for
the cases that weren't of the trivial 'access_ok(VERIFY_xyz' form.
There were a couple of notable cases:
- csky still had the old "verify_area()" name as an alias.
- the iter_iov code had magical hardcoded knowledge of the actual
values of VERIFY_{READ,WRITE} (not that they mattered, since nothing
really used it)
- microblaze used the type argument for a debug printout
but other than those oddities this should be a total no-op patch.
I tried to fix up all architectures, did fairly extensive grepping for
access_ok() uses, and the changes are trivial, but I may have missed
something. Any missed conversion should be trivially fixable, though.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-01-04 10:57:57 +08:00
|
|
|
!access_ok(addr, nb)))
|
2007-08-25 05:42:53 +08:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
/* userland only */
|
|
|
|
if (unlikely(!user_mode(regs)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
flush_spe_to_thread(current);
|
|
|
|
|
|
|
|
/* If we are loading, get the data from user space, else
|
|
|
|
* get it from register values
|
|
|
|
*/
|
|
|
|
if (flags & ST) {
|
|
|
|
data.ll = 0;
|
|
|
|
switch (instr) {
|
|
|
|
case EVSTDD:
|
|
|
|
case EVSTDW:
|
|
|
|
case EVSTDH:
|
|
|
|
data.w[0] = *evr;
|
|
|
|
data.w[1] = regs->gpr[reg];
|
|
|
|
break;
|
|
|
|
case EVSTWHE:
|
|
|
|
data.h[2] = *evr >> 16;
|
|
|
|
data.h[3] = regs->gpr[reg] >> 16;
|
|
|
|
break;
|
|
|
|
case EVSTWHO:
|
|
|
|
data.h[2] = *evr & 0xffff;
|
|
|
|
data.h[3] = regs->gpr[reg] & 0xffff;
|
|
|
|
break;
|
|
|
|
case EVSTWWE:
|
|
|
|
data.w[1] = *evr;
|
|
|
|
break;
|
|
|
|
case EVSTWWO:
|
|
|
|
data.w[1] = regs->gpr[reg];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
temp.ll = data.ll = 0;
|
|
|
|
ret = 0;
|
|
|
|
p = addr;
|
|
|
|
|
|
|
|
switch (nb) {
|
|
|
|
case 8:
|
|
|
|
ret |= __get_user_inatomic(temp.v[0], p++);
|
|
|
|
ret |= __get_user_inatomic(temp.v[1], p++);
|
|
|
|
ret |= __get_user_inatomic(temp.v[2], p++);
|
|
|
|
ret |= __get_user_inatomic(temp.v[3], p++);
|
2020-07-28 06:42:01 +08:00
|
|
|
fallthrough;
|
2007-08-25 05:42:53 +08:00
|
|
|
case 4:
|
|
|
|
ret |= __get_user_inatomic(temp.v[4], p++);
|
|
|
|
ret |= __get_user_inatomic(temp.v[5], p++);
|
2020-07-28 06:42:01 +08:00
|
|
|
fallthrough;
|
2007-08-25 05:42:53 +08:00
|
|
|
case 2:
|
|
|
|
ret |= __get_user_inatomic(temp.v[6], p++);
|
|
|
|
ret |= __get_user_inatomic(temp.v[7], p++);
|
|
|
|
if (unlikely(ret))
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (instr) {
|
|
|
|
case EVLDD:
|
|
|
|
case EVLDW:
|
|
|
|
case EVLDH:
|
|
|
|
data.ll = temp.ll;
|
|
|
|
break;
|
|
|
|
case EVLHHESPLAT:
|
|
|
|
data.h[0] = temp.h[3];
|
|
|
|
data.h[2] = temp.h[3];
|
|
|
|
break;
|
|
|
|
case EVLHHOUSPLAT:
|
|
|
|
case EVLHHOSSPLAT:
|
|
|
|
data.h[1] = temp.h[3];
|
|
|
|
data.h[3] = temp.h[3];
|
|
|
|
break;
|
|
|
|
case EVLWHE:
|
|
|
|
data.h[0] = temp.h[2];
|
|
|
|
data.h[2] = temp.h[3];
|
|
|
|
break;
|
|
|
|
case EVLWHOU:
|
|
|
|
case EVLWHOS:
|
|
|
|
data.h[1] = temp.h[2];
|
|
|
|
data.h[3] = temp.h[3];
|
|
|
|
break;
|
|
|
|
case EVLWWSPLAT:
|
|
|
|
data.w[0] = temp.w[1];
|
|
|
|
data.w[1] = temp.w[1];
|
|
|
|
break;
|
|
|
|
case EVLWHSPLAT:
|
|
|
|
data.h[0] = temp.h[2];
|
|
|
|
data.h[1] = temp.h[2];
|
|
|
|
data.h[2] = temp.h[3];
|
|
|
|
data.h[3] = temp.h[3];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & SW) {
|
|
|
|
switch (flags & 0xf0) {
|
|
|
|
case E8:
|
2013-09-23 10:04:46 +08:00
|
|
|
data.ll = swab64(data.ll);
|
2007-08-25 05:42:53 +08:00
|
|
|
break;
|
|
|
|
case E4:
|
2013-09-23 10:04:46 +08:00
|
|
|
data.w[0] = swab32(data.w[0]);
|
|
|
|
data.w[1] = swab32(data.w[1]);
|
2007-08-25 05:42:53 +08:00
|
|
|
break;
|
|
|
|
/* Its half word endian */
|
|
|
|
default:
|
2013-09-23 10:04:46 +08:00
|
|
|
data.h[0] = swab16(data.h[0]);
|
|
|
|
data.h[1] = swab16(data.h[1]);
|
|
|
|
data.h[2] = swab16(data.h[2]);
|
|
|
|
data.h[3] = swab16(data.h[3]);
|
2007-08-25 05:42:53 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & SE) {
|
|
|
|
data.w[0] = (s16)data.h[1];
|
|
|
|
data.w[1] = (s16)data.h[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store result to memory or update registers */
|
|
|
|
if (flags & ST) {
|
|
|
|
ret = 0;
|
|
|
|
p = addr;
|
|
|
|
switch (nb) {
|
|
|
|
case 8:
|
|
|
|
ret |= __put_user_inatomic(data.v[0], p++);
|
|
|
|
ret |= __put_user_inatomic(data.v[1], p++);
|
|
|
|
ret |= __put_user_inatomic(data.v[2], p++);
|
|
|
|
ret |= __put_user_inatomic(data.v[3], p++);
|
2020-07-28 06:42:01 +08:00
|
|
|
fallthrough;
|
2007-08-25 05:42:53 +08:00
|
|
|
case 4:
|
|
|
|
ret |= __put_user_inatomic(data.v[4], p++);
|
|
|
|
ret |= __put_user_inatomic(data.v[5], p++);
|
2020-07-28 06:42:01 +08:00
|
|
|
fallthrough;
|
2007-08-25 05:42:53 +08:00
|
|
|
case 2:
|
|
|
|
ret |= __put_user_inatomic(data.v[6], p++);
|
|
|
|
ret |= __put_user_inatomic(data.v[7], p++);
|
|
|
|
}
|
|
|
|
if (unlikely(ret))
|
|
|
|
return -EFAULT;
|
|
|
|
} else {
|
|
|
|
*evr = data.w[0];
|
|
|
|
regs->gpr[reg] = data.w[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_SPE */
|
2005-11-18 11:09:41 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Called on alignment exception. Attempts to fixup
|
|
|
|
*
|
|
|
|
* Return 1 on success
|
|
|
|
* Return 0 if unable to handle the interrupt
|
|
|
|
* Return -EFAULT if data address is bad
|
2017-08-30 12:12:40 +08:00
|
|
|
* Other negative return values indicate that the instruction can't
|
|
|
|
* be emulated, and the process should be given a SIGBUS.
|
2005-11-18 11:09:41 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
int fix_alignment(struct pt_regs *regs)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2020-05-06 11:40:31 +08:00
|
|
|
struct ppc_inst instr;
|
2017-08-30 12:12:40 +08:00
|
|
|
struct instruction_op op;
|
|
|
|
int r, type;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
2005-11-18 11:09:41 +08:00
|
|
|
* We require a complete register set, if not, then our assembly
|
|
|
|
* is broken
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2005-11-18 11:09:41 +08:00
|
|
|
CHECK_FULL_REGS(regs);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2020-05-06 11:40:36 +08:00
|
|
|
if (unlikely(__get_user_instr(instr, (void __user *)regs->nip)))
|
2017-08-30 12:12:40 +08:00
|
|
|
return -EFAULT;
|
|
|
|
if ((regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE)) {
|
|
|
|
/* We don't handle PPC little-endian any more... */
|
|
|
|
if (cpu_has_feature(CPU_FTR_PPC_LE))
|
|
|
|
return -EIO;
|
2020-05-06 11:40:29 +08:00
|
|
|
instr = ppc_inst_swab(instr);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-08-25 05:42:53 +08:00
|
|
|
#ifdef CONFIG_SPE
|
2020-05-06 11:40:28 +08:00
|
|
|
if (ppc_inst_primary_opcode(instr) == 0x4) {
|
2020-05-06 11:40:27 +08:00
|
|
|
int reg = (ppc_inst_val(instr) >> 21) & 0x1f;
|
2009-10-28 02:46:55 +08:00
|
|
|
PPC_WARN_ALIGNMENT(spe, regs);
|
2007-08-25 05:42:53 +08:00
|
|
|
return emulate_spe(regs, reg, instr);
|
2009-05-18 10:10:05 +08:00
|
|
|
}
|
2007-08-25 05:42:53 +08:00
|
|
|
#endif
|
|
|
|
|
2016-06-17 07:33:45 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ISA 3.0 (such as P9) copy, copy_first, paste and paste_last alignment
|
|
|
|
* check.
|
|
|
|
*
|
|
|
|
* Send a SIGBUS to the process that caused the fault.
|
|
|
|
*
|
|
|
|
* We do not emulate these because paste may contain additional metadata
|
|
|
|
* when pasting to a co-processor. Furthermore, paste_last is the
|
|
|
|
* synchronisation point for preceding copy/paste sequences.
|
|
|
|
*/
|
2020-05-06 11:40:27 +08:00
|
|
|
if ((ppc_inst_val(instr) & 0xfc0006fe) == (PPC_INST_COPY & 0xfc0006fe))
|
2016-06-17 07:33:45 +08:00
|
|
|
return -EIO;
|
|
|
|
|
2017-08-30 12:12:40 +08:00
|
|
|
r = analyse_instr(&op, regs, instr);
|
|
|
|
if (r < 0)
|
|
|
|
return -EINVAL;
|
2005-11-18 11:09:41 +08:00
|
|
|
|
2018-05-21 12:21:06 +08:00
|
|
|
type = GETTYPE(op.type);
|
2017-08-30 12:12:40 +08:00
|
|
|
if (!OP_IS_LOAD_STORE(type)) {
|
2017-09-13 12:51:24 +08:00
|
|
|
if (op.type != CACHEOP + DCBZ)
|
2017-08-30 12:12:40 +08:00
|
|
|
return -EINVAL;
|
|
|
|
PPC_WARN_ALIGNMENT(dcbz, regs);
|
|
|
|
r = emulate_dcbz(op.ea, regs);
|
|
|
|
} else {
|
|
|
|
if (type == LARX || type == STCX)
|
|
|
|
return -EIO;
|
|
|
|
PPC_WARN_ALIGNMENT(unaligned, regs);
|
|
|
|
r = emulate_loadstore(regs, &op);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2005-11-18 11:09:41 +08:00
|
|
|
|
2017-08-30 12:12:40 +08:00
|
|
|
if (!r)
|
|
|
|
return 1;
|
|
|
|
return r;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|