linux-pci.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/2] pci: imx6: avoid dereferencing program counter from user mode
@ 2018-12-04 13:27 Stefan Agner
  2018-12-04 13:27 ` [PATCH v2 2/2] pci: imx6: support kernels built in Thumb-2 mode Stefan Agner
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Stefan Agner @ 2018-12-04 13:27 UTC (permalink / raw)
  To: hongxing.zhu, l.stach
  Cc: robin.murphy, tpiepho, linux, leonard.crestez, andrew.smirnov,
	festevam, lorenzo.pieralisi, bhelgaas, stefan, linux-pci,
	linux-arm-kernel, linux-kernel

The custom fault handler is currently only meant to handle kernel
mode bus faults. Exit in case the abort happened in user mode.

Signed-off-by: Stefan Agner <stefan@agner.ch>
---
 drivers/pci/controller/dwc/pci-imx6.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/controller/dwc/pci-imx6.c b/drivers/pci/controller/dwc/pci-imx6.c
index 69f86234f7c0..54a29e441303 100644
--- a/drivers/pci/controller/dwc/pci-imx6.c
+++ b/drivers/pci/controller/dwc/pci-imx6.c
@@ -270,8 +270,14 @@ static int imx6q_pcie_abort_handler(unsigned long addr,
 		unsigned int fsr, struct pt_regs *regs)
 {
 	unsigned long pc = instruction_pointer(regs);
-	unsigned long instr = *(unsigned long *)pc;
-	int reg = (instr >> 12) & 15;
+	unsigned long instr;
+	int reg;
+
+	if (user_mode(regs))
+		return 1;
+
+	instr = *(unsigned long *)pc;
+	reg = (instr >> 12) & 15;
 
 	/*
 	 * If the instruction being executed was a read,
-- 
2.19.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/2] pci: imx6: support kernels built in Thumb-2 mode
  2018-12-04 13:27 [PATCH v2 1/2] pci: imx6: avoid dereferencing program counter from user mode Stefan Agner
@ 2018-12-04 13:27 ` Stefan Agner
  2019-02-11 16:57   ` Lucas Stach
  2019-02-08 12:13 ` [PATCH v2 1/2] pci: imx6: avoid dereferencing program counter from user mode Lorenzo Pieralisi
  2019-02-11 16:56 ` Lucas Stach
  2 siblings, 1 reply; 5+ messages in thread
From: Stefan Agner @ 2018-12-04 13:27 UTC (permalink / raw)
  To: hongxing.zhu, l.stach
  Cc: robin.murphy, tpiepho, linux, leonard.crestez, andrew.smirnov,
	festevam, lorenzo.pieralisi, bhelgaas, stefan, linux-pci,
	linux-arm-kernel, linux-kernel

Add a fault handler which handles immediate reads in Thumb-2
mode. Install the appropriate handler depending on which mode
the kernel has been built. This avoids an "Unhandled fault:
external abort on non-linefetch (0x1008) at 0xf0a80000"
during boot on a device with a PCIe switch connected.

Link: https://lore.kernel.org/linux-pci/20181126161645.8177-1-stefan@agner.ch/
Signed-off-by: Stefan Agner <stefan@agner.ch>
---
Changes since v1:
- Added Thumb-2 32-bit instruction support (tested by inserting .w
  instructions in arch/arm/include/asm/io.h)
- Avoid dereferencing if fault happened in user mode

 drivers/pci/controller/dwc/pci-imx6.c | 59 ++++++++++++++++++++++++++-
 1 file changed, 58 insertions(+), 1 deletion(-)

diff --git a/drivers/pci/controller/dwc/pci-imx6.c b/drivers/pci/controller/dwc/pci-imx6.c
index 54a29e441303..a9bbbf176c4a 100644
--- a/drivers/pci/controller/dwc/pci-imx6.c
+++ b/drivers/pci/controller/dwc/pci-imx6.c
@@ -29,6 +29,7 @@
 #include <linux/reset.h>
 #include <linux/pm_domain.h>
 #include <linux/pm_runtime.h>
+#include <asm/opcodes.h>
 
 #include "pcie-designware.h"
 
@@ -305,6 +306,59 @@ static int imx6q_pcie_abort_handler(unsigned long addr,
 	return 1;
 }
 
+static int imx6q_pcie_abort_handler_thumb2(unsigned long addr,
+		unsigned int fsr, struct pt_regs *regs)
+{
+	unsigned long pc = instruction_pointer(regs);
+	unsigned long instr;
+
+	if (user_mode(regs))
+		return 1;
+
+	instr = __mem_to_opcode_thumb32(*(unsigned long *)pc);
+
+	if (__opcode_is_thumb32(instr)) {
+		/* Load word/byte and halfword immediate offset */
+		if ((instr & 0xff100000UL) == 0xf8100000UL) {
+			int reg = (instr >> 12) & 0xf;
+			unsigned long val;
+
+			if ((instr & 0x00700000UL) == 0x00100000UL)
+				val = 0xff;
+			else if ((instr & 0x00700000UL) == 0x00300000UL)
+				val = 0xffff;
+			else
+				val = 0xffffffffUL;
+
+			regs->uregs[reg] = val;
+			regs->ARM_pc += 4;
+			return 0;
+		}
+	} else {
+		instr = __mem_to_opcode_thumb16(*(unsigned long *)pc);
+
+		/* Load word/byte and halfword immediate offset */
+		if (((instr & 0xe800) == 0x6800) ||
+		    ((instr & 0xf800) == 0x8800)) {
+			int reg = instr & 0x7;
+			unsigned long val;
+
+			if (instr & 0x1000)
+				val = 0xff;
+			else if (instr & 0x8000)
+				val = 0xffff;
+			else
+				val = 0xffffffffUL;
+
+			regs->uregs[reg] = val;
+			regs->ARM_pc += 2;
+			return 0;
+		}
+	}
+
+	return 1;
+}
+
 static int imx6_pcie_attach_pd(struct device *dev)
 {
 	struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
@@ -1075,6 +1129,8 @@ static struct platform_driver imx6_pcie_driver = {
 
 static int __init imx6_pcie_init(void)
 {
+	bool thumb2 = IS_ENABLED(CONFIG_THUMB2_KERNEL);
+
 	/*
 	 * Since probe() can be deferred we need to make sure that
 	 * hook_fault_code is not called after __init memory is freed
@@ -1082,7 +1138,8 @@ static int __init imx6_pcie_init(void)
 	 * we can install the handler here without risking it
 	 * accessing some uninitialized driver state.
 	 */
-	hook_fault_code(8, imx6q_pcie_abort_handler, SIGBUS, 0,
+	hook_fault_code(8, thumb2 ? imx6q_pcie_abort_handler_thumb2 :
+			imx6q_pcie_abort_handler, SIGBUS, 0,
 			"external abort on non-linefetch");
 
 	return platform_driver_register(&imx6_pcie_driver);
-- 
2.19.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] pci: imx6: avoid dereferencing program counter from user mode
  2018-12-04 13:27 [PATCH v2 1/2] pci: imx6: avoid dereferencing program counter from user mode Stefan Agner
  2018-12-04 13:27 ` [PATCH v2 2/2] pci: imx6: support kernels built in Thumb-2 mode Stefan Agner
@ 2019-02-08 12:13 ` Lorenzo Pieralisi
  2019-02-11 16:56 ` Lucas Stach
  2 siblings, 0 replies; 5+ messages in thread
From: Lorenzo Pieralisi @ 2019-02-08 12:13 UTC (permalink / raw)
  To: Stefan Agner
  Cc: hongxing.zhu, l.stach, robin.murphy, tpiepho, linux,
	leonard.crestez, andrew.smirnov, festevam, bhelgaas, linux-pci,
	linux-arm-kernel, linux-kernel

On Tue, Dec 04, 2018 at 02:27:32PM +0100, Stefan Agner wrote:
> The custom fault handler is currently only meant to handle kernel
> mode bus faults. Exit in case the abort happened in user mode.
> 
> Signed-off-by: Stefan Agner <stefan@agner.ch>
> ---
>  drivers/pci/controller/dwc/pci-imx6.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)

If this series is still aimed at mainline I need Lucas' ACK to
merge it.

Lorenzo

> diff --git a/drivers/pci/controller/dwc/pci-imx6.c b/drivers/pci/controller/dwc/pci-imx6.c
> index 69f86234f7c0..54a29e441303 100644
> --- a/drivers/pci/controller/dwc/pci-imx6.c
> +++ b/drivers/pci/controller/dwc/pci-imx6.c
> @@ -270,8 +270,14 @@ static int imx6q_pcie_abort_handler(unsigned long addr,
>  		unsigned int fsr, struct pt_regs *regs)
>  {
>  	unsigned long pc = instruction_pointer(regs);
> -	unsigned long instr = *(unsigned long *)pc;
> -	int reg = (instr >> 12) & 15;
> +	unsigned long instr;
> +	int reg;
> +
> +	if (user_mode(regs))
> +		return 1;
> +
> +	instr = *(unsigned long *)pc;
> +	reg = (instr >> 12) & 15;
>  
>  	/*
>  	 * If the instruction being executed was a read,
> -- 
> 2.19.1
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] pci: imx6: avoid dereferencing program counter from user mode
  2018-12-04 13:27 [PATCH v2 1/2] pci: imx6: avoid dereferencing program counter from user mode Stefan Agner
  2018-12-04 13:27 ` [PATCH v2 2/2] pci: imx6: support kernels built in Thumb-2 mode Stefan Agner
  2019-02-08 12:13 ` [PATCH v2 1/2] pci: imx6: avoid dereferencing program counter from user mode Lorenzo Pieralisi
@ 2019-02-11 16:56 ` Lucas Stach
  2 siblings, 0 replies; 5+ messages in thread
From: Lucas Stach @ 2019-02-11 16:56 UTC (permalink / raw)
  To: Stefan Agner, hongxing.zhu
  Cc: robin.murphy, tpiepho, linux, leonard.crestez, andrew.smirnov,
	festevam, lorenzo.pieralisi, bhelgaas, linux-pci,
	linux-arm-kernel, linux-kernel

Am Dienstag, den 04.12.2018, 14:27 +0100 schrieb Stefan Agner:
> The custom fault handler is currently only meant to handle kernel
> mode bus faults. Exit in case the abort happened in user mode.
> 
> Signed-off-by: Stefan Agner <stefan@agner.ch>

Reviewed-by: Lucas Stach <l.stach@pengutronix.de>

> ---
>  drivers/pci/controller/dwc/pci-imx6.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/pci/controller/dwc/pci-imx6.c b/drivers/pci/controller/dwc/pci-imx6.c
> index 69f86234f7c0..54a29e441303 100644
> --- a/drivers/pci/controller/dwc/pci-imx6.c
> +++ b/drivers/pci/controller/dwc/pci-imx6.c
> @@ -270,8 +270,14 @@ static int imx6q_pcie_abort_handler(unsigned long addr,
>  		unsigned int fsr, struct pt_regs *regs)
>  {
>  	unsigned long pc = instruction_pointer(regs);
> -	unsigned long instr = *(unsigned long *)pc;
> -	int reg = (instr >> 12) & 15;
> +	unsigned long instr;
> +	int reg;
> +
> +	if (user_mode(regs))
> +		return 1;
> +
> +	instr = *(unsigned long *)pc;
> +	reg = (instr >> 12) & 15;
>  
>  	/*
>  	 * If the instruction being executed was a read,

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 2/2] pci: imx6: support kernels built in Thumb-2 mode
  2018-12-04 13:27 ` [PATCH v2 2/2] pci: imx6: support kernels built in Thumb-2 mode Stefan Agner
@ 2019-02-11 16:57   ` Lucas Stach
  0 siblings, 0 replies; 5+ messages in thread
From: Lucas Stach @ 2019-02-11 16:57 UTC (permalink / raw)
  To: Stefan Agner, hongxing.zhu
  Cc: robin.murphy, tpiepho, linux, leonard.crestez, andrew.smirnov,
	festevam, lorenzo.pieralisi, bhelgaas, linux-pci,
	linux-arm-kernel, linux-kernel

Am Dienstag, den 04.12.2018, 14:27 +0100 schrieb Stefan Agner:
> Add a fault handler which handles immediate reads in Thumb-2
> mode. Install the appropriate handler depending on which mode
> the kernel has been built. This avoids an "Unhandled fault:
> external abort on non-linefetch (0x1008) at 0xf0a80000"
> during boot on a device with a PCIe switch connected.
> 
> Link: https://lore.kernel.org/linux-pci/20181126161645.8177-1-stefan@agner.ch/
> Signed-off-by: Stefan Agner <stefan@agner.ch>

Acked-by: Lucas Stach <l.stach@pengutronix.de>

> ---
> Changes since v1:
> - Added Thumb-2 32-bit instruction support (tested by inserting .w
>   instructions in arch/arm/include/asm/io.h)
> - Avoid dereferencing if fault happened in user mode
> 
>  drivers/pci/controller/dwc/pci-imx6.c | 59 ++++++++++++++++++++++++++-
>  1 file changed, 58 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/pci/controller/dwc/pci-imx6.c b/drivers/pci/controller/dwc/pci-imx6.c
> index 54a29e441303..a9bbbf176c4a 100644
> --- a/drivers/pci/controller/dwc/pci-imx6.c
> +++ b/drivers/pci/controller/dwc/pci-imx6.c
> @@ -29,6 +29,7 @@
>  #include <linux/reset.h>
>  #include <linux/pm_domain.h>
>  #include <linux/pm_runtime.h>
> +#include <asm/opcodes.h>
>  
>  #include "pcie-designware.h"
>  
> @@ -305,6 +306,59 @@ static int imx6q_pcie_abort_handler(unsigned long addr,
>  	return 1;
>  }
>  
> +static int imx6q_pcie_abort_handler_thumb2(unsigned long addr,
> +		unsigned int fsr, struct pt_regs *regs)
> +{
> +	unsigned long pc = instruction_pointer(regs);
> +	unsigned long instr;
> +
> +	if (user_mode(regs))
> +		return 1;
> +
> +	instr = __mem_to_opcode_thumb32(*(unsigned long *)pc);
> +
> +	if (__opcode_is_thumb32(instr)) {
> +		/* Load word/byte and halfword immediate offset */
> +		if ((instr & 0xff100000UL) == 0xf8100000UL) {
> +			int reg = (instr >> 12) & 0xf;
> +			unsigned long val;
> +
> +			if ((instr & 0x00700000UL) == 0x00100000UL)
> +				val = 0xff;
> +			else if ((instr & 0x00700000UL) == 0x00300000UL)
> +				val = 0xffff;
> +			else
> +				val = 0xffffffffUL;
> +
> +			regs->uregs[reg] = val;
> +			regs->ARM_pc += 4;
> +			return 0;
> +		}
> +	} else {
> +		instr = __mem_to_opcode_thumb16(*(unsigned long *)pc);
> +
> +		/* Load word/byte and halfword immediate offset */
> +		if (((instr & 0xe800) == 0x6800) ||
> +		    ((instr & 0xf800) == 0x8800)) {
> +			int reg = instr & 0x7;
> +			unsigned long val;
> +
> +			if (instr & 0x1000)
> +				val = 0xff;
> +			else if (instr & 0x8000)
> +				val = 0xffff;
> +			else
> +				val = 0xffffffffUL;
> +
> +			regs->uregs[reg] = val;
> +			regs->ARM_pc += 2;
> +			return 0;
> +		}
> +	}
> +
> +	return 1;
> +}
> +
>  static int imx6_pcie_attach_pd(struct device *dev)
>  {
>  	struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
> @@ -1075,6 +1129,8 @@ static struct platform_driver imx6_pcie_driver = {
>  
>  static int __init imx6_pcie_init(void)
>  {
> +	bool thumb2 = IS_ENABLED(CONFIG_THUMB2_KERNEL);
> +
>  	/*
>  	 * Since probe() can be deferred we need to make sure that
>  	 * hook_fault_code is not called after __init memory is freed
> @@ -1082,7 +1138,8 @@ static int __init imx6_pcie_init(void)
>  	 * we can install the handler here without risking it
>  	 * accessing some uninitialized driver state.
>  	 */
> -	hook_fault_code(8, imx6q_pcie_abort_handler, SIGBUS, 0,
> +	hook_fault_code(8, thumb2 ? imx6q_pcie_abort_handler_thumb2 :
> +			imx6q_pcie_abort_handler, SIGBUS, 0,
>  			"external abort on non-linefetch");
>  
>  	return platform_driver_register(&imx6_pcie_driver);

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-02-11 16:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-04 13:27 [PATCH v2 1/2] pci: imx6: avoid dereferencing program counter from user mode Stefan Agner
2018-12-04 13:27 ` [PATCH v2 2/2] pci: imx6: support kernels built in Thumb-2 mode Stefan Agner
2019-02-11 16:57   ` Lucas Stach
2019-02-08 12:13 ` [PATCH v2 1/2] pci: imx6: avoid dereferencing program counter from user mode Lorenzo Pieralisi
2019-02-11 16:56 ` Lucas Stach

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).