mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v3 1/2] ARM: mmu64: allow to disable null pointer trap on zero page
@ 2020-10-21 14:51 Michael Tretter
  2020-10-21 14:51 ` [PATCH v3 2/2] uimage: disable zero page when loading to SDRAM at address 0x0 Michael Tretter
  2020-10-22  7:31 ` [PATCH v3 1/2] ARM: mmu64: allow to disable null pointer trap on zero page Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Michael Tretter @ 2020-10-21 14:51 UTC (permalink / raw)
  To: barebox; +Cc: Michael Tretter

Barebox uses the zero page to trap NULL pointer dereferences. However,
if the SDRAM starts at address 0x0, this makes the first page of the
SDRAM inaccessible and makes it impossible to load images to offset 0x0
in the SDRAM.

Trapping NULL pointer dereferences on such systems is still desirable.
Therefore, add a function to disable the traps if accessing the zero
page is necessary and to re-enable the traps after the access is done.

The zero_page_memcpy function simplifies copying to the SDRAM, because
this is the most common required functionality, but memtest also
accesses the zero page and does not use memcpy.

Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
---
v3:
- rename functions to zero_page_access and zero_page_faulting

v2:
- add a helper function for copying to or from the zero page

I am not a fan of having an architecture-specific memcpy for the zero
page, because there are other cases that need disabling of the zero
page, e.g. memtest. Therefore, I am going for a helper for memcpy, but
still expose the architecture-specific enable/disable logic.
---
 arch/arm/cpu/Kconfig  |  1 +
 arch/arm/cpu/mmu_64.c | 13 ++++++++++-
 include/zero_page.h   | 54 +++++++++++++++++++++++++++++++++++++++++++
 lib/Kconfig           |  3 +++
 4 files changed, 70 insertions(+), 1 deletion(-)
 create mode 100644 include/zero_page.h

diff --git a/arch/arm/cpu/Kconfig b/arch/arm/cpu/Kconfig
index f9f52a625260..ca3bd98962e2 100644
--- a/arch/arm/cpu/Kconfig
+++ b/arch/arm/cpu/Kconfig
@@ -89,6 +89,7 @@ config CPU_V8
 	select ARM_EXCEPTIONS
 	select GENERIC_FIND_NEXT_BIT
 	select ARCH_HAS_STACK_DUMP
+	select ARCH_HAS_ZERO_PAGE
 
 config CPU_XSC3
         bool
diff --git a/arch/arm/cpu/mmu_64.c b/arch/arm/cpu/mmu_64.c
index 7e9ae84810f6..06049e000375 100644
--- a/arch/arm/cpu/mmu_64.c
+++ b/arch/arm/cpu/mmu_64.c
@@ -10,6 +10,7 @@
 #include <init.h>
 #include <mmu.h>
 #include <errno.h>
+#include <zero_page.h>
 #include <linux/sizes.h>
 #include <asm/memory.h>
 #include <asm/pgtable64.h>
@@ -168,6 +169,16 @@ static void mmu_enable(void)
 	set_cr(get_cr() | CR_M | CR_C | CR_I);
 }
 
+void zero_page_access(void)
+{
+	create_sections(0x0, 0x0, PAGE_SIZE, CACHED_MEM);
+}
+
+void zero_page_faulting(void)
+{
+	create_sections(0x0, 0x0, PAGE_SIZE, 0x0);
+}
+
 /*
  * Prepare MMU for usage enable it.
  */
@@ -194,7 +205,7 @@ void __mmu_init(bool mmu_on)
 		create_sections(bank->start, bank->start, bank->size, CACHED_MEM);
 
 	/* Make zero page faulting to catch NULL pointer derefs */
-	create_sections(0x0, 0x0, 0x1000, 0x0);
+	zero_page_faulting();
 
 	mmu_enable();
 }
diff --git a/include/zero_page.h b/include/zero_page.h
new file mode 100644
index 000000000000..ad6861f240c6
--- /dev/null
+++ b/include/zero_page.h
@@ -0,0 +1,54 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __ZERO_PAGE_H
+#define __ZERO_PAGE_H
+
+#include <common.h>
+
+#if defined CONFIG_ARCH_HAS_ZERO_PAGE
+
+/*
+ * zero_page_faulting - fault when accessing the zero page
+ */
+void zero_page_faulting(void);
+
+/*
+ * zero_page_access - allow accesses to the zero page
+ *
+ * Disable the null pointer trap on the zero page if access to the zero page
+ * is actually required. Disable the trap with care and re-enable it
+ * immediately after the access to properly trap null pointers.
+ */
+void zero_page_access(void);
+
+#else
+
+static inline void zero_page_faulting(void)
+{
+}
+
+static inline void zero_page_access(void)
+{
+}
+
+#endif
+
+static inline bool zero_page_contains(unsigned long addr)
+{
+	return addr < PAGE_SIZE;
+}
+
+/*
+ * zero_page_memcpy - copy to or from an address located in the zero page
+ */
+static inline void *zero_page_memcpy(void *dest, const void *src, size_t count)
+{
+	void *ret;
+
+	zero_page_access();
+	ret = memcpy(dest, src, count);
+	zero_page_faulting();
+
+	return ret;
+}
+
+#endif /* __ZERO_PAGE_H */
diff --git a/lib/Kconfig b/lib/Kconfig
index 887f50ff003f..e5831ecdb9a7 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -182,6 +182,9 @@ config ARCH_HAS_STACK_DUMP
 config ARCH_HAS_DATA_ABORT_MASK
 	bool
 
+config ARCH_HAS_ZERO_PAGE
+	bool
+
 config HAVE_EFFICIENT_UNALIGNED_ACCESS
 	bool
 
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

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

* [PATCH v3 2/2] uimage: disable zero page when loading to SDRAM at address 0x0
  2020-10-21 14:51 [PATCH v3 1/2] ARM: mmu64: allow to disable null pointer trap on zero page Michael Tretter
@ 2020-10-21 14:51 ` Michael Tretter
  2020-10-22  7:31 ` [PATCH v3 1/2] ARM: mmu64: allow to disable null pointer trap on zero page Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Michael Tretter @ 2020-10-21 14:51 UTC (permalink / raw)
  To: barebox; +Cc: Michael Tretter

If the SDRAM is mapped to address 0x0 and an image should be loaded to
to the SDRAM without offset, Barebox would normally trap the access as a
null pointer.

However, since Linux kernel commit cfa7ede20f13 ("arm64: set TEXT_OFFSET
to 0x0 in preparation for removing it entirely") no offset is the
default for arm64. Therefore, copying the image to 0x0 of the SDRAM is
necessary.

Disable the zero page trap for copying an image to address 0x0.

Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
---
v3:
- none

v2:
- switch to zero_page_memcpy helper function
- read file to temporary buffer before copying to page 0
---
 common/uimage.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/common/uimage.c b/common/uimage.c
index a84b8fddc4e7..9abfbcf3bac9 100644
--- a/common/uimage.c
+++ b/common/uimage.c
@@ -27,6 +27,7 @@
 #include <rtc.h>
 #include <filetype.h>
 #include <memory.h>
+#include <zero_page.h>
 
 static inline int uimage_is_multi_image(struct uimage_handle *handle)
 {
@@ -359,7 +360,10 @@ static int uimage_sdram_flush(void *buf, unsigned int len)
 		}
 	}
 
-	memcpy(uimage_buf + uimage_size, buf, len);
+	if (zero_page_contains((unsigned long)uimage_buf + uimage_size))
+		zero_page_memcpy(uimage_buf + uimage_size, buf, len);
+	else
+		memcpy(uimage_buf + uimage_size, buf, len);
 
 	uimage_size += len;
 
@@ -388,7 +392,20 @@ struct resource *file_to_sdram(const char *filename, unsigned long adr)
 			goto out;
 		}
 
-		now = read_full(fd, (void *)(res->start + ofs), BUFSIZ);
+		if (zero_page_contains(res->start + ofs)) {
+			void *tmp = malloc(BUFSIZ);
+			if (!tmp)
+				now = -ENOMEM;
+			else
+				now = read_full(fd, tmp, BUFSIZ);
+
+			if (now > 0)
+				zero_page_memcpy((void *)(res->start + ofs), tmp, now);
+			free(tmp);
+		} else {
+			now = read_full(fd, (void *)(res->start + ofs), BUFSIZ);
+		}
+
 		if (now < 0) {
 			release_sdram_region(res);
 			res = NULL;
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

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

* Re: [PATCH v3 1/2] ARM: mmu64: allow to disable null pointer trap on zero page
  2020-10-21 14:51 [PATCH v3 1/2] ARM: mmu64: allow to disable null pointer trap on zero page Michael Tretter
  2020-10-21 14:51 ` [PATCH v3 2/2] uimage: disable zero page when loading to SDRAM at address 0x0 Michael Tretter
@ 2020-10-22  7:31 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2020-10-22  7:31 UTC (permalink / raw)
  To: Michael Tretter; +Cc: barebox

On Wed, Oct 21, 2020 at 04:51:39PM +0200, Michael Tretter wrote:
> Barebox uses the zero page to trap NULL pointer dereferences. However,
> if the SDRAM starts at address 0x0, this makes the first page of the
> SDRAM inaccessible and makes it impossible to load images to offset 0x0
> in the SDRAM.
> 
> Trapping NULL pointer dereferences on such systems is still desirable.
> Therefore, add a function to disable the traps if accessing the zero
> page is necessary and to re-enable the traps after the access is done.
> 
> The zero_page_memcpy function simplifies copying to the SDRAM, because
> this is the most common required functionality, but memtest also
> accesses the zero page and does not use memcpy.
> 
> Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>

Applied, thanks

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

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

end of thread, other threads:[~2020-10-22  7:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-21 14:51 [PATCH v3 1/2] ARM: mmu64: allow to disable null pointer trap on zero page Michael Tretter
2020-10-21 14:51 ` [PATCH v3 2/2] uimage: disable zero page when loading to SDRAM at address 0x0 Michael Tretter
2020-10-22  7:31 ` [PATCH v3 1/2] ARM: mmu64: allow to disable null pointer trap on zero page Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox