Add IMR driver for Keem Bay
cover Add IMR driver for Keem Bay
The following is a patch for a new Intel Movidius SoC, code-named Keem Bay.
Keem Bay needs a driver to disable the Isolated Memory Region (IMR) set up by the SoC bootloader during early boot.
If such an IMR is not disabled and some device tries to access it, the system will reboot.
Since this driver is SoC-specific and Keem Bay is a new SoC, I was unsure of where to put this driver. In the end I decided to create a new 'keembay' directory in 'drivers/soc'. I hope that's reasonable, if not, just let me know.
Daniele Alessandrelli (1):
soc: keembay: Add Keem Bay IMR driver
MAINTAINERS | 5 ++++ drivers/soc/Kconfig | 1 + drivers/soc/Makefile | 1 + drivers/soc/keembay/Kconfig | 22 +++++++++++++++++ drivers/soc/keembay/Makefile | 5 ++++ drivers/soc/keembay/keembay-imr.c | 40 +++++++++++++++++++++++++++++++ 6 files changed, 74 insertions(+) create mode 100644 drivers/soc/keembay/Kconfig create mode 100644 drivers/soc/keembay/Makefile create mode 100644 drivers/soc/keembay/keembay-imr.c
Signature · 1 line
2.21.1
PATCH 1/1 soc: keembay: Add Keem Bay IMR driver
From: Daniele Alessandrelli <[email protected]>
Keem Bay bootloader sets up a temporary Isolated Memory Region (IMR) to protect itself during pre-Linux boot.
This temporary IMR remains active even when control is passed to the Linux Kernel. It is Kernel responsibility to remove such an IMR during initialization.
This driver adds such functionality.
The driver is loaded during early_init, which should ensure that the IMR is removed before devices that may try to access the IMR are initialized.
--- MAINTAINERS | 5 ++++ drivers/soc/Kconfig | 1 + drivers/soc/Makefile | 1 + drivers/soc/keembay/Kconfig | 22 +++++++++++++++++ drivers/soc/keembay/Makefile | 5 ++++ drivers/soc/keembay/keembay-imr.c | 40 +++++++++++++++++++++++++++++++ 6 files changed, 74 insertions(+) create mode 100644 drivers/soc/keembay/Kconfig create mode 100644 drivers/soc/keembay/Makefile create mode 100644 drivers/soc/keembay/keembay-imr.c
MAINTAINERS +5 -0
@@ -9194,6 +9194,11 @@ S: Maintained W: http://lse.sourceforge.net/kdump/ F: Documentation/admin-guide/kdump/ +KEEMBAY IMR +M: Daniele Alessandrelli <[email protected]> +S: Maintained +F: drivers/soc/keembay/keembay-imr.c + KEENE FM RADIO TRANSMITTER DRIVER M: Hans Verkuil <[email protected]> L: [email protected]
drivers/soc/Kconfig +1 -0
@@ -9,6 +9,7 @@ source "drivers/soc/bcm/Kconfig" source "drivers/soc/fsl/Kconfig" source "drivers/soc/imx/Kconfig" source "drivers/soc/ixp4xx/Kconfig" +source "drivers/soc/keembay/Kconfig" source "drivers/soc/mediatek/Kconfig" source "drivers/soc/qcom/Kconfig" source "drivers/soc/renesas/Kconfig"
drivers/soc/Makefile +1 -0
@@ -13,6 +13,7 @@ obj-y += fsl/ obj-$(CONFIG_ARCH_GEMINI) += gemini/ obj-y += imx/ obj-$(CONFIG_ARCH_IXP4XX) += ixp4xx/ +obj-y += keembay/ obj-$(CONFIG_SOC_XWAY) += lantiq/ obj-y += mediatek/ obj-y += amlogic/
drivers/soc/keembay/Kconfig new file +22 -0
@@ -0,0 +1,22 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Keem Bay SoC drivers. +# + +menu "Keem Bay SoC drivers" + +config KEEMBAY_IMR + bool "Clean-up Keem Bay bootloader IMR at boot" + depends on ARM64 + help + This option makes the Kernel clean up the Isolated Memory Region + (IMR) set up by Keem Bay bootloader (U-boot) to protect itself during + early boot. + + The IMR number to be cleaned up is taken from the device tree + (property 'u-boot-imr' of the 'chosen' node). + + If you are compiling the Kernel for a Keem Bay SoC select Y, + otherwise select N. + +endmenu
drivers/soc/keembay/Makefile new file +5 -0
@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Makefile for Keem Bay SoC drivers. +# +obj-$(CONFIG_KEEMBAY_IMR) += keembay-imr.o
drivers/soc/keembay/keembay-imr.c new file +40 -0
@@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2019-2020 Intel Corporation +*/ + +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <linux/arm-smccc.h> +#include <linux/init.h> +#include <linux/of.h> +#include <linux/printk.h> +#include <linux/types.h> + +/* Keem Bay SiP SVC for clearing an IMR. */ +#define KMB_SIP_SVC_IMR_CLEAR 0x8200ff13 + +static int __init clear_imr (u64 imr) +{ +struct arm_smccc_res res = {0 }; + +arm_smccc_smc (KMB_SIP_SVC_IMR_CLEAR , imr,0 ,0 ,0 ,0 ,0 ,0 , &res); + +return res.a0; +} + +static int __init kmb_imr_init (void ) +{ +u32 imr; +int rc; + + rc =of_property_read_u32 (of_chosen,"u-boot-imr" , &imr); +if (rc) { +pr_warn ("Skipping IMR clean-up: No U-Boot IMR defined in device tree\n" ); +return 0 ; + } +pr_info ("Disabling Keem Bay U-boot IMR: %u\n" , imr); + +return clear_imr (imr); +} +early_initcall (kmb_imr_init);
Signature · 1 line
2.21.1
Quoted 20 lines · Daniele Alessandrelli
The following is a patch for a new Intel Movidius SoC, code-named Keem
Bay.Keem Bay needs a driver to disable the Isolated Memory Region (IMR) set up by the SoC bootloader during early boot.
If such an IMR is not disabled and some device tries to access it, the system will reboot.
Since this driver is SoC-specific and Keem Bay is a new SoC, I was unsure of where to put this driver. In the end I decided to create a new 'keembay' directory in '
drivers/soc'. I hope that's reasonable, if
not, just let me know.Daniele Alessandrelli (1):
soc: keembay: Add Keem Bay IMR driver
Quoted 12 lines
MAINTAINERS | 5 ++++ drivers/soc/Kconfig | 1 + drivers/soc/Makefile | 1 + drivers/soc/keembay/Kconfig | 22 +++++++++++++++++ drivers/soc/keembay/Makefile | 5 ++++ drivers/soc/keembay/keembay-imr.c | 40 +++++++++++++++++++++++++++++++ 6 files changed, 74 insertions(+) create mode 100644 drivers/soc/keembay/Kconfig create mode 100644 drivers/soc/keembay/Makefile create mode 100644 drivers/soc/keembay/keembay-imr.c
Adding some more people (Arnd and linux-arm-kernel ML) in CC in the hope of getting some guidance on how to have this patch merged.
I'm a novice, so I wonder if the lack of feedback is because I'm doing something wrong or if I just sent the initial email to the wrong people / ML.
I'd appreciate any help you can provide.
--------------------------------------------------------------
Intel Research and Development Ireland Limited
Registered in Ireland
Registered Office: Collinstown Industrial Park, Leixlip, County Kildare Registered Number: 308263
This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.
Quoted 4 lines · Daniele
This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.
This footer means I delete all of your emails...
Quoted 11 lines · On Fri, 2020-05-01 at 09:09 +0200, [email protected] wrote:
Quoted 7 lines · Daniele
This e-mail and any attachments may contain confidential material for the sole
use of the intended recipient(s). Any review or distribution by others is
strictly prohibited. If you are not the intended recipient, please contact the
sender and delete all copies.This footer means I delete all of your emails...
My bad, I replied using the wrong email address (i.e., the one that adds the footer automatically). Sorry about that :/
I'll be more careful the next time.
The original emails (the ones with the cover letter and the patch) were fine though (unless I did something else wrong). Any advice on how to have the patch reviewed and eventually merged?
Quoted 13 lines · Daniele Alessandrelli
Quoted 11 lines · On Fri, 2020-05-01 at 09:09 +0200, [email protected] wrote:
Quoted 7 lines · Daniele
This e-mail and any attachments may contain confidential material for the sole
use of the intended recipient(s). Any review or distribution by others is
strictly prohibited. If you are not the intended recipient, please contact the
sender and delete all copies.This footer means I delete all of your emails...
Quoted 14 lines
My bad, I replied using the wrong email address (i.e., the one that adds the footer automatically). Sorry about that :/
I'll be more careful the next time.The original emails (the ones with the cover letter and the patch) were fine though (unless I did something else wrong). Any advice on how to have the patch reviewed and eventually merged?
Ok, let me go look at the code...
Re: [PATCH 1/1] soc: keembay: Add Keem Bay IMR driver
In reply to Daniele AlessandrelliQuoted 9 lines · Daniele Alessandrelli
From: Daniele Alessandrelli <[email protected]>
Keem Bay bootloader sets up a temporary Isolated Memory Region (IMR) to protect itself during pre-Linux boot.
This temporary IMR remains active even when control is passed to the Linux Kernel. It is Kernel responsibility to remove such an IMR during initialization.
Quoted 7 lines
This driver adds such functionality.
The driver is loaded during
early_init, which should ensure that the IMR is removed before devices that may try to access the IMR are initialized.Signed-off-by Daniele Alessandrelli <[email protected]>
First off, there is a "proper" way to send patches to the kernel community that Intel has that I do not think you are following. Please work with the Intel "Linux group" to do that first, as odds are they would have caught all of these issues beforehand (hint, which is why that process is in place...)
Quoted 33 lines
--- MAINTAINERS | 5 ++++ drivers/soc/Kconfig | 1 + drivers/soc/Makefile | 1 + drivers/soc/keembay/Kconfig | 22 +++++++++++++++++ drivers/soc/keembay/Makefile | 5 ++++ drivers/soc/keembay/keembay-imr.c | 40 +++++++++++++++++++++++++++++++ 6 files changed, 74 insertions(+) create mode 100644 drivers/soc/keembay/Kconfig create mode 100644 drivers/soc/keembay/Makefile create mode 100644 drivers/soc/keembay/keembay-imr.cMAINTAINERS +5
-0@@ -9194,6 +9194,11 @@ S: Maintained W: http://lse.sourceforge.net/kdump/ F: Documentation/admin-guide/kdump/ +KEEMBAY IMR +M: Daniele Alessandrelli <[email protected]> +S: Maintained +F: drivers/soc/keembay/keembay-imr.c + KEENE FM RADIO TRANSMITTER DRIVER M: Hans Verkuil <[email protected]> L: [email protected]drivers/soc/Kconfig +0
-0
drivers/soc/Kconfig source "drivers/soc/bcm/Kconfig" · line 12
Quoted 4 lines
drivers/soc/Kconfig +1
-0@@ -9,6 +9,7 @@ source "drivers/soc/bcm/Kconfig" source "drivers/soc/fsl/Kconfig" source "drivers/soc/imx/Kconfig" source "drivers/soc/ixp4xx/Kconfig" +source "drivers/soc/keembay/Kconfig"
For a single 40 line driver, do not make a new directory. If you end up with lots in the future, then just move the files. Don't over-engineer we have no idea what will actually happen in the future.
Quoted 31 lines
drivers/soc/Kconfig +0
-0source "drivers/soc/mediatek/Kconfig" source "drivers/soc/qcom/Kconfig" source "drivers/soc/renesas/Kconfig"drivers/soc/Makefile +1
-0@@ -13,6 +13,7 @@ obj-y += fsl/ obj-$(CONFIG_ARCH_GEMINI) += gemini/ obj-y += imx/ obj-$(CONFIG_ARCH_IXP4XX) += ixp4xx/ +obj-y += keembay/ obj-$(CONFIG_SOC_XWAY) += lantiq/ obj-y += mediatek/ obj-y += amlogic/drivers/soc/keembay/Kconfig new file +10
-0@@ -0,0 +1,22 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Keem Bay SoC drivers. +# + +menu "Keem Bay SoC drivers" + +config KEEMBAY_IMR + bool "Clean-up Keem Bay bootloader IMR at boot" + depends on ARM64
drivers/soc/keembay/Kconfig line 20
Quoted 10 lines
drivers/soc/keembay/Kconfig new file +10
-0+ help + This option makes the Kernel clean up the Isolated Memory Region + (IMR) set up by Keem Bay bootloader (U-boot) to protect itself during + early boot. + + The IMR number to be cleaned up is taken from the device tree + (property 'u-boot-imr' of the 'chosen' node). + + If you are compiling the Kernel for a Keem Bay SoC select Y, + otherwise select N.
No module support?
What about kernels that support more than one ARM device in the same kernel, you just broke that :(
Quoted 40 lines
drivers/soc/keembay/Kconfig new file +2
-0+ +endmenudrivers/soc/keembay/Makefile new file +5
-0@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Makefile for Keem Bay SoC drivers. +# +obj-$(CONFIG_KEEMBAY_IMR) += keembay-imr.odrivers/soc/keembay/keembay-imr.c new file +21
-0@@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2019-2020 Intel Corporation +*/ + +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <linux/arm-smccc.h> +#include <linux/init.h> +#include <linux/of.h> +#include <linux/printk.h> +#include <linux/types.h> + +/* Keem Bay SiP SVC for clearing an IMR. */ +#define KMB_SIP_SVC_IMR_CLEAR 0x8200ff13 + +static int __init clear_imr (u64 imr) +{ +struct arm_smccc_res res = {0 }; + +arm_smccc_smc (KMB_SIP_SVC_IMR_CLEAR , imr,0 ,0 ,0 ,0 ,0 ,0 , &res);
drivers/soc/keembay/keembay-imr.c line 31
Quoted 10 lines
drivers/soc/keembay/keembay-imr.c new file +10
-0+ +return res.a0; +} + +static int __init kmb_imr_init (void ) +{ +u32 imr; +int rc; + + rc =of_property_read_u32 (of_chosen,"u-boot-imr" , &imr);
Is there a device tree documetnation for this attribute? You have to have that approved before you can get your code merged.
drivers/soc/keembay/keembay-imr.c line 36
Quoted 5 lines
drivers/soc/keembay/keembay-imr.c new file +5
-0+if (rc) { +pr_warn ("Skipping IMR clean-up: No U-Boot IMR defined in device tree\n" ); +return 0 ; + } +pr_info ("Disabling Keem Bay U-boot IMR: %u\n" , imr);
Drivers are quiet if all is good, don't be noisy for no reason.
drivers/soc/keembay/keembay-imr.c line 38
Quoted 2 lines
drivers/soc/keembay/keembay-imr.c new file +2
-0+ +return clear_imr (imr);
Did that really need to be a separate function?
drivers/soc/keembay/keembay-imr.c line 40
Quoted 2 lines
drivers/soc/keembay/keembay-imr.c new file +2
-0+} +early_initcall (kmb_imr_init);
Like I said above, you just broke multi-system kernels by always trying to do this. Trigger off of a hardware device that only your platform has in order to properly load and run. As-is, you don't want to do this.
Anyway, Intel owes me more liquor for this review as the resources you had to get review of this were obviously not taken advantage of.
greg k-h
Re: [PATCH 1/1] soc: keembay: Add Keem Bay IMR driver
Thanks for your feedback.
Quoted 8 lines
First off, there is a "proper" way to send patches to the kernel community that Intel has that I do not think you are following. Please
work with the Intel "Linux group" to do that first, as odds are they would have caught all of these issues beforehand (hint, which is why that process is in place...)
I actually followed that process and got the OK to try to upstream.
I think the issues you identified went uncaught mainly due to the limited Linux ARM expertise within that group (and Intel in general).
Anyway, I'll keep working with our Linux Kernel people to try to reduce the burden on you and the other kernel maintainers.
Quoted 35 lines
Quoted 22 lines
drivers/soc/keembay/Kconfig new file +12
-0@@ -0,0 +1,22 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Keem Bay SoC drivers. +# + +menu "Keem Bay SoC drivers" + +config KEEMBAY_IMR + bool "Clean-up Keem Bay bootloader IMR at boot" + depends on ARM64 + help + This option makes the Kernel clean up the Isolated MemoryRegion
drivers/soc/keembay/Kconfig new file +1
-0+ (IMR) set up by Keem Bay bootloader (U-boot) to protectitself during
drivers/soc/keembay/Kconfig line 20
Quoted 7 lines
drivers/soc/keembay/Kconfig new file +7
-0+ early boot. + + The IMR number to be cleaned up is taken from the device tree + (property 'u-boot-imr' of the 'chosen' node). + + If you are compiling the Kernel for a Keem Bay SoC select Y, + otherwise select N.No module support?
What about kernels that support more than one ARM device in the same kernel, you just broke that :(
<cut>
Quoted 9 lines
drivers/soc/keembay/Kconfig
Quoted 3 lines
drivers/soc/keembay/Kconfig new file +2
-0+} +early_initcall(kmb_imr_init);Like I said above, you just broke multi-system kernels by always trying
to do this. Trigger off of a hardware device that only your platform has in order to properly load and run. As-is, you don't want to do this.
My bad, I didn't consider the issue of multi-platform ARM kernels.
The problem is that I need this code to be run early at boot, so I don't think I can make this a module.
But I'm sure there is a proper way to achieve what I need without breaking multi-system kernels. I'll do my homework and try to find it.
Quoted 6 lines
Anyway, Intel owes me more liquor for this review as the resources you
had to get review of this were obviously not taken advantage of.greg k-h
Re: [PATCH 1/1] soc: keembay: Add Keem Bay IMR driver
Quoted 11 lines · Daniele Alessandrelli
Thanks for your feedback.
Quoted 8 lines
First off, there is a "proper" way to send patches to the kernel community that Intel has that I do not think you are following. Please
work with the Intel "Linux group" to do that first, as odds are they would have caught all of these issues beforehand (hint, which is why that process is in place...)
Quoted 1 line
I actually followed that process and got the OK to try to upstream.
Then someone seriously steered you wrong as I know of one specific thing you did incorrectly (hint, you needed to get them to sign off on the patch...)
Quoted 2 lines
I think the issues you identified went uncaught mainly due to the limited Linux ARM expertise within that group (and Intel in general).
No, they should know better, if not, there are bigger problems there :(
greg k-h
Re: [PATCH 1/1] soc: keembay: Add Keem Bay IMR driver
In reply to Daniele AlessandrelliQuoted 4 lines · Daniele Alessandrelli
From: Daniele Alessandrelli <[email protected]>
Keem Bay bootloader sets up a temporary Isolated Memory Region (IMR) to protect itself during pre-Linux boot.
What kind of bootloader is the SoC using? Sounds like bootloader responsibility to me...
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [PATCH 1/1] soc: keembay: Add Keem Bay IMR driver
In reply to Daniele AlessandrelliHi!
Quoted 10 lines
Quoted 5 lines
Like I said above, you just broke multi-system kernels by always trying
to do this. Trigger off of a hardware device that only your platform has in order to properly load and run. As-is, you don't want to do this.My bad, I didn't consider the issue of multi-platform ARM kernels.
The problem is that I need this code to be run early at boot, so I don't think I can make this a module.
How early is early enough?
What bootloader are you using?
I believe you should simply fix your bootloader not to pass locked memory to the kernel.
Alternatively, take that memory off the "memory available" maps, and only re-add it once it is usable.
Anything else is dangerous.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [PATCH 1/1] soc: keembay: Add Keem Bay IMR driver
Quoted 16 lines · Pavel Machek
Hi!
Quoted 10 lines
Quoted 5 lines
Like I said above, you just broke multi-system kernels by always trying
to do this. Trigger off of a hardware device that only your platform has in order to properly load and run. As-is, you don't want to do this.My bad, I didn't consider the issue of multi-platform ARM kernels.
The problem is that I need this code to be run early at boot, so I don't think I can make this a module.
How early is early enough?
Quoted 8 lines
What bootloader are you using?
I believe you should simply fix your bootloader not to pass locked memory to the kernel.
Alternatively, take that memory off the "memory available" maps, and only re-add it once it is usable.
Anything else is dangerous.
Agreed, this sounds like an incompatible extension of the boot protocol that we should otherwise not merge.
However, there is also a lot of missing information here, and it is always possible they are trying to something for a good reason. As long as the problem that the bootloader is trying to solve is explained well enough in the changelog, we can discuss it to see how it should be done properly.
Arnd
Re: [PATCH 1/1] soc: keembay: Add Keem Bay IMR driver
Hi Arnd, Pavel,
Thanks for your feedback.
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
The problem is that I need this code to be run early at boot, so I
don't think I can make this a module.How early is early enough?
Basically, before any device with direct memory access is activated (because if anybody, except the ARM CPU, tries to access that memory, the memory protection mechanism will be triggered and a reboot will happen).
Quoted 2 lines
Quoted 2 lines
What bootloader are you using?
U-Boot
Quoted 3 lines
Quoted 3 lines
I believe you should simply fix your bootloader not to pass locked memory to the kernel.
The bootloader is behaving like that for security reasons, so we'd like to avoid modifying it. I'll provide more information below.
Quoted 6 lines
Quoted 6 lines
Alternatively, take that memory off the "memory available" maps, and only re-add it once
it is usable.Anything else is dangerous.
That sounds like an interesting solution, thanks!
Do you know any code that I can use as a reference?
Since, the protected memory is just a few megabytes large, I guess that in theory we could just have U-Boot mark it as reserved memory and forget about it; but if there's a way to re-add it back once in Linux that would be great.
Quoted 5 lines
Agreed, this sounds like an incompatible extension of the boot protocol
that we should otherwise not merge.
Quoted 8 lines
However, there is also a lot of missing information here, and it is always
possible they are trying to something for a good reason. As long as the
problem that the bootloader is trying to solve is explained well enough
in the changelog, we can discuss it to see how it should be done properly.
Apologies, I should have provided more information. Here it is :)
Basically, at boot time U-Boot code and core memory (.text, .data, .bss, etc.) is protected by this Isolated Memory Region (IMR) which prevents any device or processing units other than the ARM CPU to access/modify the memory.
This is done for security reasons, to reduce the risks that a potential attacker can use "hijacked" HW devices to interfere with the boot process (and break the secure boot flow in place).
Before booting the Kernel, U-Boot sets up a new IMR to protect the Kernel image (so that the kernel can benefit of a similar protection) and then starts the kernel, delegating to the kernel the task of switching off the U-Boot IMR.
U-Boot doesn't turn off its own IMR because doing so would leave a (tiny) window in which the boot execution flow is not protected.
If you have any additional questions or feedback, just let me know.
Regards,
Daniele
--------------------------------------------------------------
Intel Research and Development Ireland Limited
Registered in Ireland
Registered Office: Collinstown Industrial Park, Leixlip, County Kildare Registered Number: 308263
This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.
Re: [PATCH 1/1] soc: keembay: Add Keem Bay IMR driver
Quoted 6 lines · Daniele
Quoted 5 lines
Quoted 5 lines
Alternatively, take that memory off the "memory available" maps, and only re-add it once
it is usable.Anything else is dangerous.
Quoted 8 lines
That sounds like an interesting solution, thanks!
Do you know any code that I can use as a reference?
Since, the protected memory is just a few megabytes large, I guess that in theory we could just have U-Boot mark it as reserved memory and forget about it; but if there's a way to re-add it back once in Linux that would be great.
Adding it back later on with a loadable device driver should not be a problem, as this is not a violation of the boot protocol.
Quoted 25 lines
Quoted 8 lines
Agreed, this sounds like an incompatible extension of the boot protocol that we should otherwise not merge.
However, there is also a lot of missing information here, and it is always possible they are trying to something for a good reason. As long as the problem that the bootloader is trying to solve is explained well enough in the changelog, we can discuss it to see how it should be done properly.
Apologies, I should have provided more information. Here it is :)
Basically, at boot time U-Boot code and core memory (.text, .data, .bss, etc.) is protected by this Isolated Memory Region (IMR) which prevents any device or processing units other than the ARM CPU to access/modify the memory.
This is done for security reasons, to reduce the risks that a potential attacker can use "hijacked" HW devices to interfere with the boot process (and break the secure boot flow in place).
Before booting the Kernel, U-Boot sets up a new IMR to protect the Kernel image (so that the kernel can benefit of a similar protection) and then starts the kernel, delegating to the kernel the task of switching off the U-Boot IMR.
Quoted 4 lines
U-Boot doesn't turn off its own IMR because doing so would leave a (tiny) window in which the boot execution flow is not protected.
If you have any additional questions or feedback, just let me know.
Thank you for the detailed explanation. I've never seen this done in the Linux boot flow, but I can see how it helps prevent certain kinds of attacks.
It seems that just reserving the u-boot area and optionally freeing it later from a driver solves most of your problem. I have one related question though: if the kernel itself is protected, does that mean that any driver that does a DMA to statically allocated memory inside of the kernel is broken as well? I think this is something that a couple of USB drivers do, though it is fairly rare. Does u-boot protect both only the executable sections of the kernel or also data, and does the hardware block both read and write on the IMR, or just writes?
Arnd
Re: [PATCH 1/1] soc: keembay: Add Keem Bay IMR driver
Quoted 20 lines · Arnd Bergmann
Quoted 9 lines · Daniele
Quoted 6 lines
Quoted 6 lines
Alternatively, take that memory off the "memory available" maps,
and only re-add it once
it is usable.Anything else is dangerous.
That sounds like an interesting solution, thanks!
Quoted 8 lines
Do you know any code that I can use as a reference?
Since, the protected memory is just a few megabytes large, I guess that
in theory we could just have U-Boot mark it as reserved memory and forget about it; but if there's a way to re-add it back once in Linux
that would be great.
Quoted 2 lines
Adding it back later on with a loadable device driver should not be a problem, as this is not a violation of the boot protocol.
Cool, I'll try to do that then, thanks!
I see two ways to do that though:
1. Create a device driver that gets a reference to the memory region from its DT node and then re-adds the memory to the pool of available memory.
2. Use a special "compatible" string for my memory region and create a driver to handle it.
However, I think that in the second case the driver must be builtin. Would that be okay?
Also, from a quick look, it seems that I can re-add that memory back by calling memblock_free() (or a similar memblock_* function). Am I on the right track?
Quoted 38 lines
Quoted 29 lines
Quoted 10 lines
Agreed, this sounds like an incompatible extension of the boot protocol that we should otherwise not merge.
However, there is also a lot of missing information here, and it is
always possible they are trying to something for a good reason. As long as the problem that the bootloader is trying to solve is explained well enough in the changelog, we can discuss it to see how it should be done properly.Apologies, I should have provided more information. Here it is :)
Basically, at boot time U-Boot code and core memory (.text, .data, .bss, etc.) is protected by this Isolated Memory Region (IMR) which prevents any device or processing units other than the ARM CPU to access/modify the memory.
This is done for security reasons, to reduce the risks that a potential
attacker can use "hijacked" HW devices to interfere with the boot process (and break the secure boot flow in place).Before booting the Kernel, U-Boot sets up a new IMR to protect the Kernel image (so that the kernel can benefit of a similar protection)
and then starts the kernel, delegating to the kernel the task of switching off the U-Boot IMR.Quoted 4 lines
U-Boot doesn't turn off its own IMR because doing so would leave a (tiny) window in which the boot execution flow is not protected.
If you have any additional questions or feedback, just let me know.
Thank you for the detailed explanation. I've never seen this done in the Linux boot flow, but I can see how it helps prevent certain kinds of attacks.
Quoted 10 lines
It seems that just reserving the u-boot area and optionally freeing it later from a driver solves most of your problem. I have one related question though: if the kernel itself is protected, does that mean that any driver that does a DMA to statically allocated memory inside of the kernel is broken as well? I think this is something that a couple of USB drivers do, though it is fairly rare. Does u-boot protect both only the executable sections of the kernel or also data, and does the hardware block both read and write on the IMR, or just writes?
Yes, you're very right. Drivers that do DMA transfers to statically allocated memory inside the kernel will be broken.
We are currently seeing this with our eMMC driver.
Current solution is to add the eMMC controller to the list of allowed "agents" for the IMR. This will reduce the level of protection, but since we expect to have only a few of these exceptions (since, as you pointed out, drivers that do DMA to static kernel memory seem to be quite rare), we think that there is still value in having the Kernel IMR.
Do you see any issue with this?
Regards,
Daniele
Re: [PATCH 1/1] soc: keembay: Add Keem Bay IMR driver
Quoted 8 lines · Daniele Alessandrelli
Quoted 4 lines · Arnd Bergmann
On Wed, May 27, 2020 at 3:31 PM Alessandrelli, Daniele <[email protected]> wrote:
Adding it back later on with a loadable device driver should not be a problem, as this is not a violation of the boot protocol.
Cool, I'll try to do that then, thanks!
Quoted 8 lines
I see two ways to do that though:
1. Create a device driver that gets a reference to the memory region from its DT node and then re-adds the memory to the pool of available memory.
2. Use a special "compatible" string for my memory region and create a driver to handle it.
I think the first approach is more common.
Quoted 2 lines
However, I think that in the second case the driver must be builtin. Would that be okay?
It's better to avoid that.
Quoted 3 lines
Also, from a quick look, it seems that I can re-add that memory back by calling
memblock_free()(or a similar memblock_* function). Am I on the right track?
I'm not sure if memblock_free() works after early memory initialization is complete, but I think there is some way to do it later. Maybe try memblock_free() first, and then look for something else if it doesn't work.
Quoted 16 lines
Quoted 10 lines
It seems that just reserving the u-boot area and optionally freeing it later from a driver solves most of your problem. I have one related question though: if the kernel itself is protected, does that mean that any driver that does a DMA to statically allocated memory inside of the kernel is broken as well? I think this is something that a couple of USB drivers do, though it is fairly rare. Does u-boot protect both only the executable sections of the kernel or also data, and does the hardware block both read and write on the IMR, or just writes?
Yes, you're very right. Drivers that do DMA transfers to statically allocated memory inside the kernel will be broken.
We are currently seeing this with our eMMC driver.
Quoted 8 lines
Current solution is to add the eMMC controller to the list of allowed "agents" for the IMR. This will reduce the level of protection, but since we expect to have only a few of these exceptions (since, as you pointed out, drivers that do DMA to static kernel memory seem to be quite rare), we think that there is still value in having the Kernel IMR.
Do you see any issue with this?
I think you should try to fix the driver rather than making an exception for it. Hot-pluggable drivers are a much more interesting case I think, because on the one hand you have no idea what users might want to plug in legitimately, but on the other hand those are also the most likely attack vectors (driver bugs for random USB drivers overwriting kernel memory when faced with malicious hardware) that this feature is trying to prevent.
I also wonder whether we should do something in the normal iommu code that prevents one from mapping a page that the kernel would consider as protected (kernel .text, freed memory, ...) into the iommu in the first place.
Arnd
Re: [PATCH 1/1] soc: keembay: Add Keem Bay IMR driver
In reply to Alessandrelli, DanieleHi!
Quoted 16 lines
Quoted 4 lines
Agreed, this sounds like an incompatible extension of the boot protocol
that we should otherwise not merge.Quoted 8 lines
However, there is also a lot of missing information here, and it is always
possible they are trying to something for a good reason. As long as the
problem that the bootloader is trying to solve is explained well enough
in the changelog, we can discuss it to see how it should be done properly.Apologies, I should have provided more information. Here it is :)
Quoted 8 lines
Basically, at boot time U-Boot code and core memory (.text, .data, .bss, etc.) is protected by this Isolated Memory Region (IMR) which prevents any device or processing units other than the ARM CPU to access/modify the memory.
This is done for security reasons, to reduce the risks that a potential attacker can use "hijacked" HW devices to interfere with the boot process (and break the secure boot flow in place).
Dunno. You disable that after boot anyway. Whether it is disabled just before starting kernel or just after it makes very little difference.
Plus, I'm not sure if this has much security value at all. If I can corrupt data u-boot works _with_ (such as kernel, dtb), I'll take over the system anyway.
IOW I believe the best/simplest way is to simply disable this in u-boot before jumping to kernel entrypoint.
Best regards,
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [PATCH 1/1] soc: keembay: Add Keem Bay IMR driver
In reply to Arnd BergmannQuoted 38 lines · Arnd Bergmann
Quoted 18 lines · Daniele Alessandrelli
Quoted 6 lines · Arnd Bergmann
On Wed, May 27, 2020 at 3:31 PM Alessandrelli, Daniele < [email protected]> wrote:
Adding it back later on with a loadable device driver should not be a problem, as this is not a violation of the boot protocol.
Cool, I'll try to do that then, thanks!
I see two ways to do that though:
1. Create a device driver that gets a reference to the memory region
from its DT node and then re-adds the memory to the pool of available
memory.Quoted 3 lines
2. Use a special "compatible" string for my memory region and create a
driver to handle it.I think the first approach is more common.
Quoted 3 lines
However, I think that in the second case the driver must be builtin.
Would that be okay?It's better to avoid that.
Quoted 5 lines
Also, from a quick look, it seems that I can re-add that memory back by
callingmemblock_free()(or a similar memblock_* function). Am I on the
right track?
Quoted 6 lines
I'm not sure if
memblock_free()works after early memory initialization
is complete, but I think there is some way to do it later. Maybe trymemblock_free()first, and then look for something else if it doesn't work.
Brilliant. I will create a new patch using the 1st approach and see if memblock_free() works; if not, I will look for something else.
Thanks a lot for your valuable feedback and advice.
Quoted 26 lines
Quoted 26 lines
Quoted 10 lines
It seems that just reserving the u-boot area and optionally freeing it later from a driver solves most of your problem. I have one related question though: if the kernel itself is protected, does that mean that any driver that does a DMA to statically allocated memory inside of the kernel is broken as well? I think this is something that a couple of USB drivers do, though it is fairly rare. Does u-boot protect both only the executable sections of the kernel or also data, and does the hardware block both read and write on the IMR, or just writes?
Yes, you're very right. Drivers that do DMA transfers to statically allocated memory inside the kernel will be broken.
We are currently seeing this with our eMMC driver.
Current solution is to add the eMMC controller to the list of allowed
"agents" for the IMR. This will reduce the level of protection, but since we expect to have only a few of these exceptions (since, as you
pointed out, drivers that do DMA to static kernel memory seem to be quite rare), we think that there is still value in having the Kernel
IMR.
Quoted 4 lines
Quoted 1 line
Do you see any issue with this?
I think you should try to fix the driver rather than making an exception for it.
Yes, we'll look into that.
Quoted 11 lines
Hot-pluggable drivers are a much more interesting case I think, because on the one hand you have no idea what users might want to plug in legitimately, but on the other hand those are also the most likely attack vectors (driver bugs for random USB drivers overwriting kernel memory when faced with malicious hardware) that this feature is trying to prevent.
I also wonder whether we should do something in the normal iommu code that prevents one from mapping a page that the kernel would consider as protected (kernel .text, freed memory, ...) into the iommu in the first place.
Sounds like an iteresting security feature; expecially because it would apply to any hardware.
Quoted 2 lines
Arnd
Re: [PATCH 1/1] soc: keembay: Add Keem Bay IMR driver
In reply to Pavel MachekQuoted 24 lines · Pavel Machek
Hi!
Quoted 22 lines
Quoted 4 lines
Agreed, this sounds like an incompatible extension of the boot protocol
that we should otherwise not merge.Quoted 10 lines
However, there is also a lot of missing information here, and it is
always
possible they are trying to something for a good reason. As long as
the
problem that the bootloader is trying to solve is explained well enough
in the changelog, we can discuss it to see how it should be done properly.Apologies, I should have provided more information. Here it is :)
Basically, at boot time U-Boot code and core memory (.text, .data, .bss, etc.) is protected by this Isolated Memory Region (IMR) which prevents any device or processing units other than the ARM CPU to access/modify the memory.
Quoted 8 lines
Quoted 4 lines
This is done for security reasons, to reduce the risks that a potential
attacker can use "hijacked" HW devices to interfere with the boot process (and break the secure boot flow in place).Dunno. You disable that after boot anyway. Whether it is disabled just before starting kernel or just after it makes very little difference.
Not sure I get your point. Disabling it while U-Boot is still running poses a security risk (even if arguably tiny), while doing it once the the Kernel is running is totally safe. So, I'd prefer to do it in the Kernel, unless practical reasons prevent it.
Quoted 4 lines
Plus, I'm not sure if this has much security value at all. If I can corrupt data u-boot works _with_ (such as kernel, dtb), I'll take over the system anyway.
True, U-Boot data needs to be protected too and, in fact, we're trying to do that as well. Other IMRs are used to protect the kernel, dtb, and other critical memory sections.
Quoted 3 lines
IOW I believe the best/simplest way is to simply disable this in u-boot before jumping to kernel entrypoint.
Yes, that's definitely the simplest solution, but, IMO, not the safest one. So, I'd prefer to build on your initial suggestion and Arnd's advice and create a new device driver to disable the IMR once Linux is running. But, yes, if that eventually proves unfeasible, I might just have the bootloader disable the protection right before booting the OS.
Quoted 5 lines
Best regards,
Pavel