Skip to contentSkip to conversation

Devicetree support for UML

PATCH In review Vincent Whitchurch 4 participants 10 messages Dec 8, 2021 – Jan 18, 2022 User-mode Linux (UML)

lore
Share & tools
lore
#0 lore raw

cover Devicetree support for UML

This series add support for passing a devicetree blob to UML. It can be used for testing device drivers.

Vincent Whitchurch (2):
um: Extract load file helper from initrd.c
um: Add devicetree support

 arch/um/Kconfig            |  1 +
 arch/um/kernel/Makefile    |  2 ++
 arch/um/kernel/dtb.c       | 41 +++++++++++++++++++++++++
 arch/um/kernel/initrd.c    | 48 ++++--------------------------
 arch/um/kernel/load_file.c | 61 ++++++++++++++++++++++++++++++++++++++
 arch/um/kernel/um_arch.c   |  3 ++
 arch/um/kernel/um_arch.h   | 14 +++++++++
 7 files changed, 127 insertions(+), 43 deletions(-)
 create mode 100644 arch/um/kernel/dtb.c
 create mode 100644 arch/um/kernel/load_file.c
 create mode 100644 arch/um/kernel/um_arch.h
Signature · 1 line
2.33.1
#1 lore raw

PATCH 1/2 um: Extract load file helper from initrd.c

The file loading support in initrd.c can be re-used for loading devicetrees. Move it out of initrd.c.

---
 arch/um/kernel/Makefile    |  1 +
 arch/um/kernel/initrd.c    | 48 ++++--------------------------
 arch/um/kernel/load_file.c | 61 ++++++++++++++++++++++++++++++++++++++
 arch/um/kernel/um_arch.h   |  8 +++++
 4 files changed, 75 insertions(+), 43 deletions(-)
 create mode 100644 arch/um/kernel/load_file.c
 create mode 100644 arch/um/kernel/um_arch.h
arch/um/kernel/Makefile +1 -0
index 1d18e4e46989..92692bfef7ae 100644
@@ -18,6 +18,7 @@ obj-y = config.o exec.o exitcode.o irq.o ksyms.o mem.o \
 	physmem.o process.o ptrace.o reboot.o sigio.o \
 	signal.o syscall.o sysrq.o time.o tlb.o trap.o \
 	um_arch.o umid.o maccess.o kmsg_dump.o capflags.o skas/
+obj-y += load_file.o
 
 obj-$(CONFIG_BLK_DEV_INITRD) += initrd.o
 obj-$(CONFIG_GPROF)	+= gprof_syms.o
arch/um/kernel/initrd.c +5 -43

67 lines not on this page · raw message

arch/um/kernel/load_file.c new file +61 -0

61 lines not on this page · raw message

arch/um/kernel/um_arch.h new file +8 -0
new file mode 100644
index 000000000000..b195df3a09a0
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __UML_ARCH_H__
+#define __UML_ARCH_H__
+
+extern void * __init uml_load_file(const char *filename, unsigned long long *size);
+
+#endif
Signature · 1 line
2.33.1
#2 lore raw

PATCH 2/2 um: Add devicetree support

In reply to Vincent Whitchurch

Add a dtb=<filename> option to boot UML with a devicetree blob. This can be used for testing driver code using UML.

---
 arch/um/Kconfig          |  1 +
 arch/um/kernel/Makefile  |  1 +
 arch/um/kernel/dtb.c     | 41 ++++++++++++++++++++++++++++++++++++++++
 arch/um/kernel/um_arch.c |  3 +++
 arch/um/kernel/um_arch.h |  6 ++++++
 5 files changed, 52 insertions(+)
 create mode 100644 arch/um/kernel/dtb.c
arch/um/Kconfig +1 -0
index c18b45f75d41..1cf7ef3a2b81 100644
@@ -18,6 +18,7 @@ config UML
 	select HAVE_DEBUG_KMEMLEAK
 	select HAVE_DEBUG_BUGVERBOSE
 	select NO_DMA if !UML_DMA_EMULATION
+	select OF_EARLY_FLATTREE
 	select GENERIC_IRQ_SHOW
 	select GENERIC_CPU_DEVICES
 	select HAVE_GCC_PLUGINS
arch/um/kernel/Makefile +1 -0
index 92692bfef7ae..ebd0cca3ff26 100644
@@ -22,6 +22,7 @@ obj-y += load_file.o
 
 obj-$(CONFIG_BLK_DEV_INITRD) += initrd.o
 obj-$(CONFIG_GPROF)	+= gprof_syms.o
+obj-$(CONFIG_OF) += dtb.o
 obj-$(CONFIG_EARLY_PRINTK) += early_printk.o
 obj-$(CONFIG_STACKTRACE) += stacktrace.o
 obj-$(CONFIG_GENERIC_PCI_IOMAP) += ioport.o
arch/um/kernel/dtb.c new file +41 -0

41 lines not on this page · raw message

arch/um/kernel/um_arch.c +3 -0
index 54447690de11..abceeabe29b9 100644
@@ -29,6 +29,8 @@
 #include <mem_user.h>
 #include <os.h>
 
+#include "um_arch.h"
+
 #define DEFAULT_COMMAND_LINE_ROOT "root=98:0"
 #define DEFAULT_COMMAND_LINE_CONSOLE "console=tty"
 
@@ -407,6 +409,7 @@ void __init setup_arch(char **cmdline_p)
 	stack_protections((unsigned long) &init_thread_info);
 	setup_physmem(uml_physmem, uml_reserved, physmem_size, highmem);
 	mem_total_pages(physmem_size, iomem_size, highmem);
+	uml_dtb_init();
 	read_initrd();
 
 	paging_init();
arch/um/kernel/um_arch.h +6 -0
index b195df3a09a0..1e07fb7ee35e 100644
@@ -5,4 +5,10 @@
 
 extern void * __init uml_load_file(const char *filename, unsigned long long *size);
 
+#ifdef CONFIG_OF
+extern void __init uml_dtb_init(void);
+#else
+static inline void uml_dtb_init(void) { }
+#endif
+
 #endif
Signature · 1 line
2.33.1
#3 lore raw

Re: [PATCH 2/2] um: Add devicetree support

Quoted 2 lines · Vincent Whitchurch

Add a dtb=<filename> option to boot UML with a devicetree blob. This can be used for testing driver code using UML.

Neat!

Quoted 59 lines
---
 arch/um/Kconfig          |  1 +
 arch/um/kernel/Makefile  |  1 +
 arch/um/kernel/dtb.c     | 41 ++++++++++++++++++++++++++++++++++++++++
 arch/um/kernel/um_arch.c |  3 +++
 arch/um/kernel/um_arch.h |  6 ++++++
 5 files changed, 52 insertions(+)
 create mode 100644 arch/um/kernel/dtb.c
arch/um/Kconfig +1 -0
index c18b45f75d41..1cf7ef3a2b81 100644
@@ -18,6 +18,7 @@ config UML
 	select HAVE_DEBUG_KMEMLEAK
 	select HAVE_DEBUG_BUGVERBOSE
 	select NO_DMA if !UML_DMA_EMULATION
+	select OF_EARLY_FLATTREE
 	select GENERIC_IRQ_SHOW
 	select GENERIC_CPU_DEVICES
 	select HAVE_GCC_PLUGINS
arch/um/kernel/Makefile +1 -0
index 92692bfef7ae..ebd0cca3ff26 100644
@@ -22,6 +22,7 @@ obj-y += load_file.o
 
 obj-$(CONFIG_BLK_DEV_INITRD) += initrd.o
 obj-$(CONFIG_GPROF)	+= gprof_syms.o
+obj-$(CONFIG_OF) += dtb.o
 obj-$(CONFIG_EARLY_PRINTK) += early_printk.o
 obj-$(CONFIG_STACKTRACE) += stacktrace.o
 obj-$(CONFIG_GENERIC_PCI_IOMAP) += ioport.o
arch/um/kernel/dtb.c new file +19 -0
new file mode 100644
index 000000000000..ca69d72025f3
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/init.h>
+#include <linux/of_fdt.h>
+#include <linux/printk.h>
+#include <linux/memblock.h>
+#include <init.h>
+
+#include "um_arch.h"
+
+static char *dtb __initdata;
+
+void uml_dtb_init(void)
+{
+	long long size;
+	void *area;
+
+	area = uml_load_file(dtb, &size);
+	if (!area)

arch/um/kernel/dtb.c line 29

Quoted 10 lines
arch/um/kernel/dtb.c new file +10 -0
+		return;
+
+	if (!early_init_dt_scan(area)) {
+		pr_err("invalid DTB %s\n", dtb);
+		memblock_free(area, size);
+		return;
+	}
+
+	unflatten_device_tree();
+	early_init_fdt_scan_reserved_mem();

These should be reversed. early_init_fdt_scan_reserved_mem() works on the flat tree. Reserved memory needs to be reserved before unflatten_device_tree() starts allocating memory. Though I imagine that doesn't really matter for UML.

Also, does the dtb end up in permanently allocated memory (i.e. not init)? It needs to be if not.

Rob

#4 lore raw

Re: [PATCH 2/2] um: Add devicetree support

Quoted 30 lines · Rob Herring
Quoted 29 lines · Vincent Whitchurch
arch/um/kernel/dtb.c new file +26 -0
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/init.h>
+#include <linux/of_fdt.h>
+#include <linux/printk.h>
+#include <linux/memblock.h>
+#include <init.h>
+
+#include "um_arch.h"
+
+static char *dtb __initdata;
+
+void uml_dtb_init(void)
+{
+	long long size;
+	void *area;
+
+	area = uml_load_file(dtb, &size);
+	if (!area)
+		return;
+
+	if (!early_init_dt_scan(area)) {
+		pr_err("invalid DTB %s\n", dtb);
+		memblock_free(area, size);
+		return;
+	}
Quoted 8 lines

arch/um/kernel/dtb.c line 29

Quoted 3 lines
arch/um/kernel/dtb.c new file +3 -0
+
+	unflatten_device_tree();
+	early_init_fdt_scan_reserved_mem();

These should be reversed. early_init_fdt_scan_reserved_mem() works on the flat tree. Reserved memory needs to be reserved before unflatten_device_tree() starts allocating memory. Though I imagine that doesn't really matter for UML.

I only tested dynamic allocation of reserved memory, and that, unsuprisingly, works regardless of the order. But I'll send a patch to change it.

Quoted 2 lines

Also, does the dtb end up in permanently allocated memory (i.e. not init)? It needs to be if not.

The dtb is allocated with memblock_alloc() in arch/um/kernel/load_file.c and is never freed (except on an error from early_init_dt_scan() as in the above hunk), so I think this is already taken care of.

#5 lore raw
In reply to Vincent Whitchurch

Vincent,

----- Ursprüngliche Mail -----

Quoted 9 lines

Von: "Vincent Whitchurch" <[email protected]>
An: "Jeff Dike" <[email protected]>, "richard" <[email protected]>, "anton ivanov" <[email protected]>
CC: [email protected], "linux-kernel" <[email protected]>, "linux-um" <[email protected]>, "devicetree" <[email protected]>, "Vincent Whitchurch" <[email protected]>
Gesendet: Mittwoch, 8. Dezember 2021 16:11:21
Betreff: [PATCH 0/2] Devicetree support for UML

This series add support for passing a devicetree blob to UML. It can be used for testing device drivers.

Nice feature.
Code looks good so far. But while building I'm facing this warning:

WARNING: unmet direct dependencies detected for OF_EARLY_FLATTREE
Depends on [n]: OF [=n]
Selected by [y]:
- UML [=y]

Please note that my UML config has CONFIG_OF=n.

Thanks,
//richard

maintainer
#6 lore raw
Quoted 16 lines · Richard Weinberger

Vincent,

----- Ursprüngliche Mail -----

Quoted 9 lines

Von: "Vincent Whitchurch" <[email protected]>
An: "Jeff Dike" <[email protected]>, "richard" <[email protected]>, "anton ivanov" <[email protected]>
CC: [email protected], "linux-kernel" <[email protected]>, "linux-um" <[email protected]>, "devicetree" <[email protected]>, "Vincent Whitchurch" <[email protected]>
Gesendet: Mittwoch, 8. Dezember 2021 16:11:21
Betreff: [PATCH 0/2] Devicetree support for UML

This series add support for passing a devicetree blob to UML. It can be used for testing device drivers.

Nice feature.
Code looks good so far. But while building I'm facing this warning:

Quoted 8 lines

WARNING: unmet direct dependencies detected for OF_EARLY_FLATTREE
Depends on [n]: OF [=n]
Selected by [y]:
- UML [=y]

Please note that my UML config has CONFIG_OF=n.

Hm. So maybe that needs to be

select OF_EARLY_FLATTREE if OF

or so?

johannes

_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um

#7 lore raw

----- Ursprüngliche Mail -----

Quoted 34 lines

Von: "Johannes Berg" <[email protected]>
An: "richard" <[email protected]>, "Vincent Whitchurch" <[email protected]>
CC: "Jeff Dike" <[email protected]>, "anton ivanov" <[email protected]>, "kernel" <[email protected]>, "linux-kernel" <[email protected]>, "linux-um" <[email protected]>, "devicetree" <[email protected]>
Gesendet: Dienstag, 21. Dezember 2021 21:56:50
Betreff: Re: [PATCH 0/2] Devicetree support for UML

Quoted 25 lines · Richard Weinberger

Vincent,

----- Ursprüngliche Mail -----

Quoted 10 lines

Von: "Vincent Whitchurch" <[email protected]>
An: "Jeff Dike" <[email protected]>, "richard" <[email protected]>, "anton ivanov" <[email protected]>
CC: [email protected], "linux-kernel" <[email protected]>, "linux-um" <[email protected]>,
"devicetree" <[email protected]>, "Vincent Whitchurch" <[email protected]>
Gesendet: Mittwoch, 8. Dezember 2021 16:11:21
Betreff: [PATCH 0/2] Devicetree support for UML

Quoted 2 lines

This series add support for passing a devicetree blob to UML. It can be used for testing device drivers.

Nice feature.
Code looks good so far. But while building I'm facing this warning:

WARNING: unmet direct dependencies detected for OF_EARLY_FLATTREE
Depends on [n]: OF [=n]
Selected by [y]:
- UML [=y]

Quoted 6 lines
Quoted 2 lines

Please note that my UML config has CONFIG_OF=n.

Hm. So maybe that needs to be

select OF_EARLY_FLATTREE if OF

Yeah, IIRC arm and mips use such a pattern too.
Vincent, what do you think?

Thanks,
//richard

#8 lore raw
Quoted 9 lines · Richard Weinberger

----- Ursprüngliche Mail -----

Quoted 8 lines

Von: "Johannes Berg" <[email protected]>
Gesendet: Dienstag, 21. Dezember 2021 21:56:50

Quoted 5 lines · Richard Weinberger

WARNING: unmet direct dependencies detected for OF_EARLY_FLATTREE
Depends on [n]: OF [=n]
Selected by [y]:
- UML [=y]

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Please note that my UML config has CONFIG_OF=n.

Hm. So maybe that needs to be

select OF_EARLY_FLATTREE if OF

Yeah, IIRC arm and mips use such a pattern too. Vincent, what do you think?

Yes, that looks like the correct fix. I've tested it and it works. Do you prefer to fix it up locally or should I repost?

#9 lore raw

Vincent,

----- Ursprüngliche Mail -----

Quoted 22 lines

Von: "Vincent Whitchurch" <[email protected]>
An: "richard" <[email protected]>
CC: "Johannes Berg" <[email protected]>, "Jeff Dike" <[email protected]>, "anton ivanov" <[email protected]>, "kernel" <[email protected]>, "linux-kernel" <[email protected]>, "linux-um" <[email protected]>, "devicetree" <[email protected]>
Gesendet: Mittwoch, 22. Dezember 2021 11:23:57
Betreff: Re: [PATCH 0/2] Devicetree support for UML

Quoted 13 lines · Richard Weinberger

----- Ursprüngliche Mail -----

Quoted 12 lines

Von: "Johannes Berg" <[email protected]>
Gesendet: Dienstag, 21. Dezember 2021 21:56:50

Quoted 7 lines · Richard Weinberger

WARNING: unmet direct dependencies detected for OF_EARLY_FLATTREE
Depends on [n]: OF [=n]
Selected by [y]:
- UML [=y]

Please note that my UML config has CONFIG_OF=n.

Hm. So maybe that needs to be

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

select OF_EARLY_FLATTREE if OF

Yeah, IIRC arm and mips use such a pattern too. Vincent, what do you think?

Yes, that looks like the correct fix. I've tested it and it works. Do you prefer to fix it up locally or should I repost?

I'll fix it myself. :-)

Thanks,
//richard