Skip to contentSkip to conversation

Embeddable Position Independent Executable

RFC PATCH In review Russ Dill 7 participants 19 messages Sep 17, 2013 – Jun 4, 2014

Subsystems (29)
lore
Share & tools
lore

Russ Dill

author
#0 lore raw

cover Embeddable Position Independent Executable

This patch adds support for and demonstrates the usage of an embedded position independent executable (PIE). The goal is to allow the use of C code in situations where carefully written position independent assembly was previously required.

The example used is the suspend/resume code for the am335x, a Texas Instruments ARM SoC. In order to save the maximum amount of power during suspend, the am335x has to perform several power saving operations after SDRAM has been disabled, and undo those steps at resume time. The am335x provides an SRAM region for the processor to execute such code.

A PIE executable was chosen because it limits the types of relocations that must be performed before the code is executed. In the case of ARM, the only required relocation type is a relative relocation of pointers.

The kernel is provided symbols into the PIE executable by way of exporting those symbols from the PIE and then importing them into vmlinux at final link time. Because the PIE is loaded dynamically at runtime, any access to PIE functions or data must pass through special accessor macros that apply the necessary offset. Code within the PIE does not have access to symbols outside of the PIE, but it still can access code and data outside the PIE so long as it is passed pointers to that code/data.

The PIE executable is provided its own set of functions required by gcc, such as memcpy, memmove, etc. The different PIE sections are collected together in the linker script as an overlay so that the kernel only needs one copy of these functions. When the PIE is loaded, the library functions and appropriate sections are copied into a genalloc pool (in the case of the example, backed by SRAM). The PIE code then applies the necessary relocations to the loaded code. Because the relocations are just relative offsets to pointers, the relocations can be reapplied to allow the code to run with the MMU enabled or disabled.

This patchset is a complete rethinking of an earlier patchset [1]. Ard Biesheuvel provided the suggestion to use the PIE executable format for storing the relocatable code within the kernel. Russell King and Dave Martin pointed out the shortcomings of my initial naive approach.

This patchset depends on Rajendra Nayak's SRAM DT-ification patch series [2], Suman Anna's am335x mailbox series [3], and a portion of Dave Gerlach's am335x suspend/resume patchset [4]. I've collected together the necessary dependances and applied this patch series on top of them here [5].

Because special ioremap variants are required on ARM for io mappings that allow code execution, the first two patches provide generic accessors for those variants. The third patch provides a DT and pdata method for instructing misc/sram to map the memory in such a way that allows code execution.

The 4th patch provides a generic set of functions for handling function pointers as addresses and vice versa. This is necessary on ARM because of the way that Thumb2 function pointers are handled by gcc. The PIE framework requires this functionality because it performs translations of function pointers.

The 5th patch is the general PIE framework. The 6th patch is the addition of ARM support for PIE. The 7th patch provides the ability of ARM to fixup PIE code on the fly. This is necessary since at suspend time the MMU will be working, but at resume time, it will be off. The 8th patch provides a predefined trampoline that utilizes the on the fly fixup.

The 9th patch configures the SRAM DT entries for am335x so that they can be easily found by the PM code, and so that they are mapped with exec enabled. The 10th patch adds PIE entries for am335x, and the 11th patch finally adds suspend/resume support for am33xx utilizing C code for suspend/resume paths.

[1] http://www.spinics.net/lists/arm-kernel/msg271525.html
[2] http://comments.gmane.org/gmane.linux.ports.arm.omap/103774
[3] http://www.spinics.net/lists/devicetree/msg00227.html
[4] http://www.spinics.net/lists/linux-omap/msg95305.html
[5] https://github.com/russdill/linux/commits/sram

Russ Dill (10):
asm-generic: io: Add exec versions of ioremap
lib: devres: Add exec versions of devm_ioremap_resource and friends
misc: SRAM: Add option to map SRAM to allow code execution
asm-generic: fncpy: Add function copying macros
PIE: Support embedding position independent executables
ARM: PIE: Add position independent executable embedding to ARM
ARM: PIE: Add support for updating PIE relocations
ARM: PIE: Add macro for generating PIE resume trampoline
ARM: dts: AM33XX: Associate SRAM with MPU and mark it exec
ARM: OMAP2+: AM33XX: Add PIE support for AM33XX

Vaibhav Bedia (1):
ARM: OMAP2+: AM33XX: Basic suspend resume support

 Documentation/devicetree/bindings/misc/sram.txt |   4 +
 Documentation/pie.txt                           | 167 ++++++++
 Makefile                                        |  17 +-
 arch/alpha/include/asm/fncpy.h                  |   1 +
 arch/arc/include/asm/fncpy.h                    |   1 +
 arch/arm/Kconfig                                |   1 +
 arch/arm/Makefile                               |   5 +
 arch/arm/boot/dts/am33xx.dtsi                   |   2 +
 arch/arm/configs/omap2plus_defconfig            |   1 +
 arch/arm/include/asm/elf.h                      |   1 +
 arch/arm/include/asm/fncpy.h                    |  76 +---
 arch/arm/include/asm/io.h                       |   2 +
 arch/arm/include/asm/pie.h                      |  42 ++
 arch/arm/include/asm/suspend.h                  |  25 ++
 arch/arm/kernel/.gitignore                      |   1 +
 arch/arm/kernel/Makefile                        |   4 +-
 arch/arm/kernel/pie.c                           |  92 +++++
 arch/arm/kernel/pie.lds.S                       |  41 ++
 arch/arm/kernel/vmlinux.lds.S                   |   2 +
 arch/arm/libpie/.gitignore                      |   3 +
 arch/arm/libpie/Makefile                        |  32 ++
 arch/arm/libpie/empty.S                         |  12 +
 arch/arm/libpie/relocate.S                      |  76 ++++
 arch/arm/mach-omap2/Kconfig                     |   7 +-
 arch/arm/mach-omap2/Makefile                    |   2 +
 arch/arm/mach-omap2/board-generic.c             |   1 +
 arch/arm/mach-omap2/common.h                    |  10 +
 arch/arm/mach-omap2/io.c                        |   5 +
 arch/arm/mach-omap2/pm.c                        |   3 +-
 arch/arm/mach-omap2/pm33xx.c                    | 486 ++++++++++++++++++++++++
 arch/arm/mach-omap2/pm33xx.h                    |  68 ++++
 arch/arm/mach-omap2/sleep33xx.c                 | 314 +++++++++++++++
 arch/arm/mach-omap2/wkup_m3.c                   | 183 +++++++++
 arch/arm/plat-omap/sram.c                       |   2 +-
 arch/arm64/include/asm/fncpy.h                  |   1 +
 arch/avr32/include/asm/fncpy.h                  |   1 +
 arch/blackfin/include/asm/fncpy.h               |   1 +
 arch/c6x/include/asm/fncpy.h                    |   1 +
 arch/cris/include/asm/fncpy.h                   |   1 +
 arch/frv/include/asm/fncpy.h                    |   1 +
 arch/h8300/include/asm/fncpy.h                  |   1 +
 arch/hexagon/include/asm/fncpy.h                |   1 +
 arch/ia64/include/asm/fncpy.h                   |   1 +
 arch/m32r/include/asm/fncpy.h                   |   1 +
 arch/m68k/include/asm/fncpy.h                   |   1 +
 arch/metag/include/asm/fncpy.h                  |   1 +
 arch/microblaze/include/asm/fncpy.h             |   1 +
 arch/mips/include/asm/fncpy.h                   |   1 +
 arch/mn10300/include/asm/fncpy.h                |   1 +
 arch/openrisc/include/asm/fncpy.h               |   1 +
 arch/parisc/include/asm/fncpy.h                 |   1 +
 arch/powerpc/include/asm/fncpy.h                |   1 +
 arch/s390/include/asm/fncpy.h                   |   1 +
 arch/score/include/asm/fncpy.h                  |   1 +
 arch/sh/include/asm/fncpy.h                     |   1 +
 arch/sparc/include/asm/fncpy.h                  |   1 +
 arch/tile/include/asm/fncpy.h                   |   1 +
 arch/um/include/asm/fncpy.h                     |   1 +
 arch/unicore32/include/asm/fncpy.h              |   1 +
 arch/x86/include/asm/fncpy.h                    |   1 +
 arch/xtensa/include/asm/fncpy.h                 |   1 +
 drivers/misc/sram.c                             |  13 +-
 include/asm-generic/fncpy.h                     | 104 +++++
 include/asm-generic/iomap.h                     |   5 +
 include/asm-generic/pie.lds.h                   |  82 ++++
 include/asm-generic/vmlinux.lds.h               |   1 +
 include/linux/device.h                          |  17 +-
 include/linux/io.h                              |   4 +
 include/linux/pie.h                             | 196 ++++++++++
 include/linux/platform_data/sram.h              |   8 +
 lib/Kconfig                                     |  14 +
 lib/Makefile                                    |   2 +
 lib/devres.c                                    |  97 ++++-
 lib/pie.c                                       | 138 +++++++
 pie/.gitignore                                  |   3 +
 pie/Makefile                                    |  85 +++++
 scripts/link-vmlinux.sh                         |  11 +-
 77 files changed, 2425 insertions(+), 71 deletions(-)
 create mode 100644 Documentation/pie.txt
 create mode 100644 arch/alpha/include/asm/fncpy.h
 create mode 100644 arch/arc/include/asm/fncpy.h
 create mode 100644 arch/arm/include/asm/pie.h
 create mode 100644 arch/arm/kernel/pie.c
 create mode 100644 arch/arm/kernel/pie.lds.S
 create mode 100644 arch/arm/libpie/.gitignore
 create mode 100644 arch/arm/libpie/Makefile
 create mode 100644 arch/arm/libpie/empty.S
 create mode 100644 arch/arm/libpie/relocate.S
 create mode 100644 arch/arm/mach-omap2/pm33xx.c
 create mode 100644 arch/arm/mach-omap2/pm33xx.h
 create mode 100644 arch/arm/mach-omap2/sleep33xx.c
 create mode 100644 arch/arm/mach-omap2/wkup_m3.c
 create mode 100644 arch/arm64/include/asm/fncpy.h
 create mode 100644 arch/avr32/include/asm/fncpy.h
 create mode 100644 arch/blackfin/include/asm/fncpy.h
 create mode 100644 arch/c6x/include/asm/fncpy.h
 create mode 100644 arch/cris/include/asm/fncpy.h
 create mode 100644 arch/frv/include/asm/fncpy.h
 create mode 100644 arch/h8300/include/asm/fncpy.h
 create mode 100644 arch/hexagon/include/asm/fncpy.h
 create mode 100644 arch/ia64/include/asm/fncpy.h
 create mode 100644 arch/m32r/include/asm/fncpy.h
 create mode 100644 arch/m68k/include/asm/fncpy.h
 create mode 100644 arch/metag/include/asm/fncpy.h
 create mode 100644 arch/microblaze/include/asm/fncpy.h
 create mode 100644 arch/mips/include/asm/fncpy.h
 create mode 100644 arch/mn10300/include/asm/fncpy.h
 create mode 100644 arch/openrisc/include/asm/fncpy.h
 create mode 100644 arch/parisc/include/asm/fncpy.h
 create mode 100644 arch/powerpc/include/asm/fncpy.h
 create mode 100644 arch/s390/include/asm/fncpy.h
 create mode 100644 arch/score/include/asm/fncpy.h
 create mode 100644 arch/sh/include/asm/fncpy.h
 create mode 100644 arch/sparc/include/asm/fncpy.h
 create mode 100644 arch/tile/include/asm/fncpy.h
 create mode 100644 arch/um/include/asm/fncpy.h
 create mode 100644 arch/unicore32/include/asm/fncpy.h
 create mode 100644 arch/x86/include/asm/fncpy.h
 create mode 100644 arch/xtensa/include/asm/fncpy.h
 create mode 100644 include/asm-generic/fncpy.h
 create mode 100644 include/asm-generic/pie.lds.h
 create mode 100644 include/linux/pie.h
 create mode 100644 include/linux/platform_data/sram.h
 create mode 100644 lib/pie.c
 create mode 100644 pie/.gitignore
 create mode 100644 pie/Makefile
Signature · 1 line
1.8.3.2
author
#1 lore raw

RFC PATCH 1/11 asm-generic: io: Add exec versions of ioremap

If code is to be copied into and area (such as SRAM) and run, it needs to be marked as exec. Currently only an ARM version of this exists.

---
 arch/arm/include/asm/io.h   | 2 ++
 include/asm-generic/iomap.h | 5 +++++
 2 files changed, 7 insertions(+)
arch/arm/include/asm/io.h +2 -0
index d070741..212d095 100644
@@ -329,6 +329,8 @@ extern void _memset_io(volatile void __iomem *, int, size_t);
 #define ioremap_nocache(cookie,size)	__arm_ioremap((cookie), (size), MT_DEVICE)
 #define ioremap_cached(cookie,size)	__arm_ioremap((cookie), (size), MT_DEVICE_CACHED)
 #define ioremap_wc(cookie,size)		__arm_ioremap((cookie), (size), MT_DEVICE_WC)
+#define ioremap_exec(cookie,size)	__arm_ioremap_exec((cookie), (size), true)
+#define ioremap_exec_nocache(cookie,size) __arm_ioremap_exec((cookie), (size), false)
 #define iounmap				__arm_iounmap
 
 /*
include/asm-generic/iomap.h +5 -0
index 6afd7d6..e72c451 100644
@@ -66,6 +66,11 @@ extern void ioport_unmap(void __iomem *);
 #define ioremap_wc ioremap_nocache
 #endif
 
+#ifndef ARCH_HAS_IOREMAP_EXEC
+#define ioremap_exec ioremap
+#define ioremap_exec_nocache ioremap_nocache
+#endif
+
 #ifdef CONFIG_PCI
 /* Destroy a virtual mapping cookie for a PCI BAR (memory or IO) */
 struct pci_dev;
Signature · 1 line
1.8.3.2
author
#2 lore raw

RFC PATCH 2/11 lib: devres: Add exec versions of devm_ioremap_resource and friends

In reply to Russ Dill

Now that there is an _exec version of ioremap, add devm support for it.

---
 include/linux/device.h | 17 ++++++++-
 include/linux/io.h     |  4 +++
 lib/devres.c           | 97 ++++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 114 insertions(+), 4 deletions(-)
include/linux/device.h +16 -1
index 22b546a..204180a 100644
@@ -596,10 +596,25 @@ extern int devres_release_group(struct device *dev, void *id);
 extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
 extern void devm_kfree(struct device *dev, void *p);
 
-void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res);
+void __iomem *__devm_ioremap_resource(struct device *dev, struct resource *res,
+				     bool exec);
+static inline void __iomem *devm_ioremap_resource(struct device *dev,
+						  struct resource *res)
+{
+	return __devm_ioremap_resource(dev, res, false);
+}
 void __iomem *devm_request_and_ioremap(struct device *dev,
 			struct resource *res);
 
+static inline void __iomem *devm_ioremap_exec_resource(struct device *dev,
+						       struct resource *res)
+{
+	return __devm_ioremap_resource(dev, res, true);
+}
+
+void __iomem *devm_request_and_ioremap_exec(struct device *dev,
+			struct resource *res);
+
 /* allows to add/remove a custom action to devres stack */
 int devm_add_action(struct device *dev, void (*action)(void *), void *data);
 void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
include/linux/io.h +4 -0
index f4f42fa..582b207 100644
@@ -62,6 +62,10 @@ void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
 			    unsigned long size);
 void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset,
 				    unsigned long size);
+void __iomem *devm_ioremap_exec(struct device *dev, resource_size_t offset,
+			    unsigned long size);
+void __iomem *devm_ioremap_exec_nocache(struct device *dev, resource_size_t offset,
+				    unsigned long size);
 void devm_iounmap(struct device *dev, void __iomem *addr);
 int check_signature(const volatile void __iomem *io_addr,
 			const unsigned char *signature, int length);
lib/devres.c +94 -3

127 lines not on this page · raw message

Signature · 1 line
1.8.3.2
author
#3 lore raw

RFC PATCH 3/11 misc: SRAM: Add option to map SRAM to allow code execution

In reply to Russ Dill

This is necessary for platforms that use SRAM to execute suspend/resume stubs.

---
 Documentation/devicetree/bindings/misc/sram.txt |  4 ++++
 drivers/misc/sram.c                             | 13 ++++++++++++-
 include/linux/platform_data/sram.h              |  8 ++++++++
 3 files changed, 24 insertions(+), 1 deletion(-)
 create mode 100644 include/linux/platform_data/sram.h
Documentation/devicetree/bindings/misc/sram.txt +4 -0
index 4d0a00e..4fa9af3 100644
@@ -8,6 +8,10 @@ Required properties:
 
 - reg : SRAM iomem address range
 
+Optional properties:
+
+- map-exec: Map range to allow code execution
+
 Example:
 
 sram: sram at 5c000000 {
drivers/misc/sram.c +12 -1
index d87cc91..baa5008 100644
@@ -28,6 +28,7 @@
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/genalloc.h>
+#include <linux/platform_data/sram.h>
 
 #define SRAM_GRANULARITY	32
 
@@ -38,14 +39,24 @@ struct sram_dev {
 
 static int sram_probe(struct platform_device *pdev)
 {
+	struct sram_platform_data *pdata = pdev->dev.platform_data;
 	void __iomem *virt_base;
 	struct sram_dev *sram;
 	struct resource *res;
 	unsigned long size;
+	bool map_exec = false;
 	int ret;
 
+	if (of_get_property(pdev->dev.of_node, "map-exec", NULL))
+		map_exec = true;
+	if (pdata && pdata->map_exec)
+		map_exec |= true;
+
 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-	virt_base = devm_ioremap_resource(&pdev->dev, res);
+	if (map_exec)
+		virt_base = devm_ioremap_exec_resource(&pdev->dev, res);
+	else
+		virt_base = devm_ioremap_resource(&pdev->dev, res);
 	if (IS_ERR(virt_base))
 		return PTR_ERR(virt_base);
 
include/linux/platform_data/sram.h new file +8 -0
new file mode 100644
index 0000000..8f5c4ba
@@ -0,0 +1,8 @@
+#ifndef _LINUX_SRAM_H
+#define _LINUX_SRAM_H
+
+struct sram_platform_data {
+	bool map_exec;
+};
+
+#endif
Signature · 1 line
1.8.3.2
author
#4 lore raw

RFC PATCH 4/11 asm-generic: fncpy: Add function copying macros

In reply to Russ Dill

Under certain arches (ARM) function pointers cannot be used naively. Specifically, for thumb functions, their 0 bit is set, but they are contained on a word aligned address.

Add a fncpy macro to perform function copies correctly along with two helpers, fnptr_to_address, and fnptr_translate.

---
 arch/alpha/include/asm/fncpy.h      |   1 +
 arch/arc/include/asm/fncpy.h        |   1 +
 arch/arm/include/asm/fncpy.h        |  76 +++++++-------------------
 arch/arm/plat-omap/sram.c           |   2 +-
 arch/arm64/include/asm/fncpy.h      |   1 +
 arch/avr32/include/asm/fncpy.h      |   1 +
 arch/blackfin/include/asm/fncpy.h   |   1 +
 arch/c6x/include/asm/fncpy.h        |   1 +
 arch/cris/include/asm/fncpy.h       |   1 +
 arch/frv/include/asm/fncpy.h        |   1 +
 arch/h8300/include/asm/fncpy.h      |   1 +
 arch/hexagon/include/asm/fncpy.h    |   1 +
 arch/ia64/include/asm/fncpy.h       |   1 +
 arch/m32r/include/asm/fncpy.h       |   1 +
 arch/m68k/include/asm/fncpy.h       |   1 +
 arch/metag/include/asm/fncpy.h      |   1 +
 arch/microblaze/include/asm/fncpy.h |   1 +
 arch/mips/include/asm/fncpy.h       |   1 +
 arch/mn10300/include/asm/fncpy.h    |   1 +
 arch/openrisc/include/asm/fncpy.h   |   1 +
 arch/parisc/include/asm/fncpy.h     |   1 +
 arch/powerpc/include/asm/fncpy.h    |   1 +
 arch/s390/include/asm/fncpy.h       |   1 +
 arch/score/include/asm/fncpy.h      |   1 +
 arch/sh/include/asm/fncpy.h         |   1 +
 arch/sparc/include/asm/fncpy.h      |   1 +
 arch/tile/include/asm/fncpy.h       |   1 +
 arch/um/include/asm/fncpy.h         |   1 +
 arch/unicore32/include/asm/fncpy.h  |   1 +
 arch/x86/include/asm/fncpy.h        |   1 +
 arch/xtensa/include/asm/fncpy.h     |   1 +
 include/asm-generic/fncpy.h         | 104 ++++++++++++++++++++++++++++++++++++
 32 files changed, 154 insertions(+), 57 deletions(-)
 create mode 100644 arch/alpha/include/asm/fncpy.h
 create mode 100644 arch/arc/include/asm/fncpy.h
 create mode 100644 arch/arm64/include/asm/fncpy.h
 create mode 100644 arch/avr32/include/asm/fncpy.h
 create mode 100644 arch/blackfin/include/asm/fncpy.h
 create mode 100644 arch/c6x/include/asm/fncpy.h
 create mode 100644 arch/cris/include/asm/fncpy.h
 create mode 100644 arch/frv/include/asm/fncpy.h
 create mode 100644 arch/h8300/include/asm/fncpy.h
 create mode 100644 arch/hexagon/include/asm/fncpy.h
 create mode 100644 arch/ia64/include/asm/fncpy.h
 create mode 100644 arch/m32r/include/asm/fncpy.h
 create mode 100644 arch/m68k/include/asm/fncpy.h
 create mode 100644 arch/metag/include/asm/fncpy.h
 create mode 100644 arch/microblaze/include/asm/fncpy.h
 create mode 100644 arch/mips/include/asm/fncpy.h
 create mode 100644 arch/mn10300/include/asm/fncpy.h
 create mode 100644 arch/openrisc/include/asm/fncpy.h
 create mode 100644 arch/parisc/include/asm/fncpy.h
 create mode 100644 arch/powerpc/include/asm/fncpy.h
 create mode 100644 arch/s390/include/asm/fncpy.h
 create mode 100644 arch/score/include/asm/fncpy.h
 create mode 100644 arch/sh/include/asm/fncpy.h
 create mode 100644 arch/sparc/include/asm/fncpy.h
 create mode 100644 arch/tile/include/asm/fncpy.h
 create mode 100644 arch/um/include/asm/fncpy.h
 create mode 100644 arch/unicore32/include/asm/fncpy.h
 create mode 100644 arch/x86/include/asm/fncpy.h
 create mode 100644 arch/xtensa/include/asm/fncpy.h
 create mode 100644 include/asm-generic/fncpy.h
arch/alpha/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/arc/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/arm/include/asm/fncpy.h +20 -56

97 lines not on this page · raw message

arch/arm/plat-omap/sram.c +1 -1
index a5bc92d..90ccd74 100644
@@ -54,7 +54,7 @@ void *omap_sram_push_address(unsigned long size)
 	}
 
 	new_ceil -= size;
-	new_ceil = ROUND_DOWN(new_ceil, FNCPY_ALIGN);
+	new_ceil = ROUND_DOWN(new_ceil, 1 << ARCH_FNCPY_ALIGN);
 	omap_sram_ceil = IOMEM(new_ceil);
 
 	return (void *)omap_sram_ceil;
arch/arm64/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/avr32/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/blackfin/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/c6x/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/cris/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/frv/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/h8300/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/hexagon/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/ia64/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/m32r/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/m68k/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/metag/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/microblaze/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/mips/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/mn10300/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/openrisc/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/parisc/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/powerpc/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/s390/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/score/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/sh/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/sparc/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/tile/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/um/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/unicore32/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/x86/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
arch/xtensa/include/asm/fncpy.h new file +1 -0
new file mode 100644
index 0000000..ee4741c
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
include/asm-generic/fncpy.h new file +104 -0

104 lines not on this page · raw message

Signature · 1 line
1.8.3.2
#5 lore raw

Re: [RFC PATCH 04/11] asm-generic: fncpy: Add function copying macros

Review line 1

Quoted 3 lines · Russ Dill

+++ b/arch/alpha/include/asm/fncpy.h

(quoted diff) +1 -0
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>

Please add

    generic-y += fncpy.h

to arch/<arch>/include/asm/Kbuild instead.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert at linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

author
#6 lore raw

RFC PATCH 5/11 PIE: Support embedding position independent executables

In reply to Russ Dill

This commit adds support for embedding PIEs into the kernel, loading them into genalloc sections, performing necessary relocations, and running code from them. This allows platforms that need to run code from SRAM, such an during suspend/resume, to develop that code in C instead of assembly.

Functions and data for each PIE should be grouped into sections with the __pie(<group>) and __pie_data(<group>) macros respectively. Any symbols or functions that are to be accessed from outside the PIE should be marked with EXPORT_PIE_SYMBOL(<sym>). For example:

static struct ddr_timings xyz_timings __pie_data(platformxyz) = {
	[...]
};

void __pie(platformxyz) xyz_ddr_on(void *addr)

{
	[...]
}

EXPORT_PIE_SYMBOL(xyz_ddr_on);

While the kernel can access exported symbols from the PIE, the PIE cannot access symbols from the kernel, but can access data from the kernel and call functions in the kernel so long as addresses are passed into the PIE.

PIEs are loaded from the kernel into a genalloc pool with pie_load_sections. pie_load_sections allocates space within the pool, copies the neccesary code/data, and performs any necessary relocations. A chunk identifier is returned for removing the PIE from the pool, and for translating symbols.

Because the PIEs are dynamically relocated, special accessors must be used to access PIE symbols from kernel code:

- kern_to_pie(chunk, ptr): Translate a PIE symbol to the virtual address
it is loaded into within the pool.

- fn_to_pie(chunk, ptr): Same as above, but for function pointers.

- sram_to_phys(chunk, addr): Translate a virtual address within a loaded PIE to a physical address.

Loading a PIE involves three main steps. First a set of common functions to cover built-ins emitted by gcc (memcpy, memmove, etc) is copied into the pool. Then the actual PIE code and data is copied into the pool. Because the PIE code is contained within an overlay with other PIEs, offsets to the common functions are maintained. Finally, relocations are performed as necessary.

---
 Documentation/pie.txt             | 167 ++++++++++++++++++++++++++++++++
 Makefile                          |  17 +++-
 include/asm-generic/pie.lds.h     |  82 ++++++++++++++++
 include/asm-generic/vmlinux.lds.h |   1 +
 include/linux/pie.h               | 196 ++++++++++++++++++++++++++++++++++++++
 lib/Kconfig                       |  14 +++
 lib/Makefile                      |   2 +
 lib/pie.c                         | 138 +++++++++++++++++++++++++++
 pie/.gitignore                    |   3 +
 pie/Makefile                      |  85 +++++++++++++++++
 scripts/link-vmlinux.sh           |  11 ++-
 11 files changed, 711 insertions(+), 5 deletions(-)
 create mode 100644 Documentation/pie.txt
 create mode 100644 include/asm-generic/pie.lds.h
 create mode 100644 include/linux/pie.h
 create mode 100644 lib/pie.c
 create mode 100644 pie/.gitignore
 create mode 100644 pie/Makefile
Documentation/pie.txt new file +167 -0
new file mode 100644
index 0000000..54a1646
@@ -0,0 +1,167 @@
+Position Independent Executables (PIEs)
+=======================================
+
+About
+=====
+
+The PIE framework is designed to allow normal C code from the kernel to be
+embedded into the kernel, loaded at arbirary addresses, and executed.
+
+A PIE is a position independent executable is a piece of self contained code
+that can be relocated to any address. Before the code is run, a simple list
+of offset based relocations has to be performed.
+
+Copyright 2013 Texas Instruments, Inc
+	Russ Dill <[email protected]>
+
+Motivation
+==========
+
+Without the PIE framework, the only way to support platforms that require
+code loaded to and run from arbitrary addresses was to write the code in
+assembly. For example, a platform may have suspend/resume steps that
+disable/enable SDRAM and must be run from on chip SRAM.
+
+In addition to the SRAM virtual address not being known at compile time
+for device tree platforms, the code must often run with the MMU enabled or
+disabled (physical vs virtual address).
+
+Design
+======
+
+The PIE code is separated into two main pieces. libpie satifies various
+function calls emitted by gcc. The kernel contains only one copy of libpie
+but whenever a PIE is loaded, a copy of libpie is copied along with the PIE
+code. The second piece is the PIE code and data marked with special PIE
+sections. At build time, libpie and the PIE sections are collected together
+into a single PIE executable:
+
+	+---------------------------------------+
+	| __pie_common_start			|
+	|	<libpie>			|
+	| __pie_common_end			|
+	+---------------------------------------+
+	| __pie_overlay_start			|
+	| +-----------------------------+	|
+	| | __pie_groupxyz_start	|	|
+	| |   <groupxyz functions/data>	|	|
+	| | __pie_groupxyz_end		|	|
+	| +-----------------------------+	|
+	| | __pie_groupabc_start	|	|
+	| |   <groupabc functions/data>	|	|
+	| | __pie_groupabc_end		|	|
+	| +-----------------------------+	|
+	| | __pie_groupijk_start	|	|
+	| |   <groupijk functions/data>	|	|
+	| | __pie_groupijk_end		|	|
+	| +-----------------------------+	|
+	| __pie_overlay_end			|
+	+---------------------------------------+
+	| <Architecture specific relocations>	|
+	+---------------------------------------+
+
+The PIE executable is then embedded into the kernel. Symbols are exported
+from the PIE executable and passed back into the kernel at link time. When
+the PIE is loaded, the memory layout then looks like the following:
+
+	+---------------------------------------+
+	| <libpie>				|
+	+---------------------------------------+
+	| <groupabc_functions/data>		|
+	+---------------------------------------+
+	| Tail (Arch specific data/relocations	|
+	+---------------------------------------+
+
+The architecture specific code is responsible for reading the relocations
+and performing the necessary fixups.
+
+Marking code/data
+=================
+
+Marking code and data for inclusing into a PIE group is done with the PIE
+section markers, __pie(<group>) and __pie_data(<group>). Any symbols that
+will be used outside of the PIE must be exported with EXPORT_PIE_SYMBOL:
+
+    static struct ddr_timings xyz_timings __pie_data(platformxyz) = {
+    	[...]
+    };
+    
+    void __pie(platformxyz) xyz_ddr_on(void *addr)
+    {
+    	[...]
+    }
+    EXPORT_PIE_SYMBOL(xyz_ddr_on);
+
+Loading PIEs
+============
+
+PIEs can be loaded into a genalloc pool (such as one backed by SRAM). The
+following functions are provided:
+
+ - pie_load_sections(pool, <group>)
+ - pie_load_sections_phys(pool, <group>)
+ - pie_free(chunk)
+
+pie_load_sections/pie_load_sections_phys load a PIE section group into the
+given pool. Any necessary fixups are peformed and a chunk identifier is
+returned. The first variant performs fixups such that the code can be run
+with the current address layout. The second (phys) variant performs fixups
+such that the code can be executed with the MMU disabled.
+
+The pie_free function unloads a PIE from a pool.
+
+Utilizing PIEs
+==============
+
+In order to translate between symbols and addresses within a loaded PIE, the
+following macros/functions are provided:
+
+ - kern_to_pie(chunk, sym)
+ - fn_to_pie(chunk, fn)
+ - pie_to_phys(chunk, addr)
+
+All three take as the first argument the chunk returned by pie_load_sections.
+Data symbols can be translated with kern_to_pie. The macro is made so that
+the type returned is the type passed:
+
+   kern_to_pie(chunk, xyz_struct_ptr)->foo = 15;
+   *kern_to_pie(chunk, &xyz_flags) = XYZ_DO_THE_THING;
+
+Because certain architectures require special handling of function pointers,
+a special varaint is provided:
+
+   ret = fn_to_pie(chunk, &xyz_ddr_on)(addr);
+   fnptr = fn_to_pie(chunk, &abc_fn);
+
+In the case that a PIE has been configured to run with the MMU disabled,
+physical addresses can be translated with pie_to_phys. For instance, if
+the resume ROM jumps to a given physical address:
+
+   trampoline = fn_to_pie(chunk, resume_trampoline);
+   writel(pie_to_phys(chunk, trampoline), XYZ_RESUME_ADDR_REG);
+
+On the Fly Fixup
+================
+
+The tail portion of the PIE can be used to store data necessary to perform
+on the fly fixups. This is necessary for code that needs to run from
+different address spaces at different times. Any on the fly fixup support
+is architecture specific.
+
+Architecture Requirements
+=========================
+
+Individual architectures must implement two functions:
+
+pie_arch_fill_tail - This function examines the architecture specific
+relocation entries and copies the ones necessary for the given PIE.
+
+pie_arch_fixup - This function performs fixups of the PIE code based
+on the tail data generated above.
+
+pie.lds - A linker script for the PIE executable must be provided.
+include/asm-generic/pie.lds.S provides a template.
+
+libpie.o - The architecture must also provide a library of functions that
+gcc may expect as a built-in, such as memcpy, memmove, etc. The list of
+functions is architecture specific.
Makefile +14 -3
index fe8204b..4791a0f 100644
@@ -396,7 +396,7 @@ export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE CFLAGS_GCOV
 export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
 export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULE
 export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL
-export KBUILD_ARFLAGS
+export KBUILD_ARFLAGS OBJCOPY_OUTPUT_FORMAT
 
 # When compiling out-of-tree modules, put MODVERDIR in the module
 # tree rather than in the kernel tree. The kernel tree might
@@ -682,6 +682,10 @@ ifeq ($(CONFIG_STRIP_ASM_SYMS),y)
 LDFLAGS_vmlinux	+= $(call ld-option, -X,)
 endif
 
+ifeq ($(CONFIG_PIE),y)
+LDFLAGS_vmlinux += --just-symbols=pie/pie.syms
+endif
+
 # Default kernel image to build when no specific target is given.
 # KBUILD_IMAGE may be overruled on the command line or
 # set in the environment
@@ -737,13 +741,15 @@ core-y		+= kernel/ mm/ fs/ ipc/ security/ crypto/ block/
 
 vmlinux-dirs	:= $(patsubst %/,%,$(filter %/, $(init-y) $(init-m) \
 		     $(core-y) $(core-m) $(drivers-y) $(drivers-m) \
-		     $(net-y) $(net-m) $(libs-y) $(libs-m)))
+		     $(net-y) $(net-m) $(libs-y) $(libs-m) $(libpie-y)))
 
 vmlinux-alldirs	:= $(sort $(vmlinux-dirs) $(patsubst %/,%,$(filter %/, \
 		     $(init-n) $(init-) \
 		     $(core-n) $(core-) $(drivers-n) $(drivers-) \
 		     $(net-n)  $(net-)  $(libs-n)    $(libs-))))
 
+pie-$(CONFIG_PIE) := pie/
+
 init-y		:= $(patsubst %/, %/built-in.o, $(init-y))
 core-y		:= $(patsubst %/, %/built-in.o, $(core-y))
 drivers-y	:= $(patsubst %/, %/built-in.o, $(drivers-y))
@@ -751,16 +757,21 @@ net-y		:= $(patsubst %/, %/built-in.o, $(net-y))
 libs-y1		:= $(patsubst %/, %/lib.a, $(libs-y))
 libs-y2		:= $(patsubst %/, %/built-in.o, $(libs-y))
 libs-y		:= $(libs-y1) $(libs-y2)
+pie-y		:= $(patsubst %/, %/built-in.o, $(pie-y))
+libpie-y	:= $(patsubst %/, %/built-in.o, $(libpie-y))
 
 # Externally visible symbols (used by link-vmlinux.sh)
 export KBUILD_VMLINUX_INIT := $(head-y) $(init-y)
 export KBUILD_VMLINUX_MAIN := $(core-y) $(libs-y) $(drivers-y) $(net-y)
+export KBUILD_VMLINUX_PIE  := $(pie-y)
+export KBUILD_LIBPIE       := $(libpie-y)
+export KBUILD_PIE_LDS      := $(PIE_LDS)
 export KBUILD_LDS          := arch/$(SRCARCH)/kernel/vmlinux.lds
 export LDFLAGS_vmlinux
 # used by scripts/pacmage/Makefile
 export KBUILD_ALLDIRS := $(sort $(filter-out arch/%,$(vmlinux-alldirs)) arch Documentation include samples scripts tools virt)
 
-vmlinux-deps := $(KBUILD_LDS) $(KBUILD_VMLINUX_INIT) $(KBUILD_VMLINUX_MAIN)
+vmlinux-deps := $(KBUILD_LDS) $(KBUILD_PIE_LDS) $(KBUILD_VMLINUX_INIT) $(KBUILD_VMLINUX_MAIN) $(KBUILD_VMLINUX_PIE)
 
 # Final link of vmlinux
       cmd_link-vmlinux = $(CONFIG_SHELL) $< $(LD) $(LDFLAGS) $(LDFLAGS_vmlinux)
include/asm-generic/pie.lds.h new file +82 -0

82 lines not on this page · raw message

include/asm-generic/vmlinux.lds.h +1 -0
index 69732d2..5a21cfe 100644
@@ -666,6 +666,7 @@
 	EXIT_CALL							\
 	*(.discard)							\
 	*(.discard.*)							\
+	*(.pie.*)							\
 	}
 
 /**
include/linux/pie.h new file +196 -0

196 lines not on this page · raw message

lib/Kconfig +14 -0
index 71d9f81..d47df14 100644
@@ -353,6 +353,20 @@ config DQL
 config NLATTR
 	bool
 
+config HAVE_PIE
+        bool
+        help
+          See Documentation/pie.txt for details.
+
+config PIE
+	bool "Embedded position independant executables"
+	depends on HAVE_PIE
+	help
+	  This option adds support for embedding position indepentant (PIE)
+	  executables into the kernel. The PIEs can then be copied into
+	  genalloc regions such as SRAM and executed. Some platforms require
+	  this for suspend/resume support.
+
 #
 # Generic 64-bit atomic support is selected if needed
 #
lib/Makefile +2 -0
index 7baccfd..2b6123d 100644
@@ -145,6 +145,8 @@ obj-$(CONFIG_GENERIC_NET_UTILS) += net_utils.o
 
 obj-$(CONFIG_STMP_DEVICE) += stmp_device.o
 
+obj-$(CONFIG_PIE) += pie.o
+
 libfdt_files = fdt.o fdt_ro.o fdt_wip.o fdt_rw.o fdt_sw.o fdt_strerror.o
 $(foreach file, $(libfdt_files), \
 	$(eval CFLAGS_$(file) = -I$(src)/../scripts/dtc/libfdt))
lib/pie.c new file +138 -0

138 lines not on this page · raw message

pie/.gitignore new file +3 -0
new file mode 100644
index 0000000..4f29803
@@ -0,0 +1,3 @@
+*.syms
+pie.lds
+pie.lds.S
pie/Makefile new file +85 -0

85 lines not on this page · raw message

scripts/link-vmlinux.sh +9 -2
index 0149949..8cf4971 100644
@@ -55,12 +55,16 @@ vmlinux_link()
 	if [ "${SRCARCH}" != "um" ]; then
 		${LD} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2}                  \
 			-T ${lds} ${KBUILD_VMLINUX_INIT}                     \
-			--start-group ${KBUILD_VMLINUX_MAIN} --end-group ${1}
+			--start-group					     \
+				${KBUILD_VMLINUX_MAIN}			     \
+				${KBUILD_VMLINUX_PIE}			     \
+			--end-group ${1}
 	else
 		${CC} ${CFLAGS_vmlinux} -o ${2}                              \
 			-Wl,-T,${lds} ${KBUILD_VMLINUX_INIT}                 \
 			-Wl,--start-group                                    \
 				 ${KBUILD_VMLINUX_MAIN}                      \
+				 ${KBUILD_VMLINUX_PIE}                       \
 			-Wl,--end-group                                      \
 			-lutil ${1}
 		rm -f linux
@@ -143,10 +147,13 @@ esac
 #link vmlinux.o
 info LD vmlinux.o
 modpost_link vmlinux.o
-
 # modpost vmlinux.o to check for section mismatches
 ${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o
 
+if [ -n "${CONFIG_PIE}" ]; then
+	${MAKE} -f "${srctree}/scripts/Makefile.build" obj=pie
+fi
+
 # Update version
 info GEN .version
 if [ ! -r .version ]; then
Signature · 1 line
1.8.3.2
author
#7 lore raw

RFC PATCH 6/11 ARM: PIE: Add position independent executable embedding to ARM

In reply to Russ Dill

Add support to ARM for embedding PIEs into the kernel, loading them into genalloc pools (such as SRAM) and executing them. Support for ARM means performing R_ARM_RELATIVE fixups within the .rel.dyn section.

---
 arch/arm/Kconfig              |  1 +
 arch/arm/Makefile             |  5 +++
 arch/arm/include/asm/elf.h    |  1 +
 arch/arm/kernel/.gitignore    |  1 +
 arch/arm/kernel/Makefile      |  4 ++-
 arch/arm/kernel/pie.c         | 83 +++++++++++++++++++++++++++++++++++++++++++
 arch/arm/kernel/pie.lds.S     | 40 +++++++++++++++++++++
 arch/arm/kernel/vmlinux.lds.S |  2 ++
 arch/arm/libpie/.gitignore    |  3 ++
 arch/arm/libpie/Makefile      | 32 +++++++++++++++++
 arch/arm/libpie/empty.S       | 12 +++++++
 11 files changed, 183 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm/kernel/pie.c
 create mode 100644 arch/arm/kernel/pie.lds.S
 create mode 100644 arch/arm/libpie/.gitignore
 create mode 100644 arch/arm/libpie/Makefile
 create mode 100644 arch/arm/libpie/empty.S
arch/arm/Kconfig +1 -0
index 43594d5..de7b7603 100644
@@ -49,6 +49,7 @@ config ARM
 	select HAVE_MEMBLOCK
 	select HAVE_OPROFILE if (HAVE_PERF_EVENTS)
 	select HAVE_PERF_EVENTS
+	select HAVE_PIE
 	select HAVE_REGS_AND_STACK_ACCESS_API
 	select HAVE_SYSCALL_TRACEPOINTS
 	select HAVE_UID16
arch/arm/Makefile +5 -0
index 6fd2cea..a673d36 100644
@@ -128,6 +128,8 @@ KBUILD_AFLAGS	+=$(CFLAGS_ABI) $(AFLAGS_ISA) $(arch-y) $(tune-y) -include asm/uni
 
 CHECKFLAGS	+= -D__arm__
 
+OBJCOPY_OUTPUT_FORMAT := elf32-littlearm
+
 #Default value
 head-y		:= arch/arm/kernel/head$(MMUEXT).o
 textofs-y	:= 0x00008000
@@ -273,6 +275,9 @@ drivers-$(CONFIG_OPROFILE)      += arch/arm/oprofile/
 
 libs-y				:= arch/arm/lib/ $(libs-y)
 
+PIE_LDS				:= arch/arm/kernel/pie.lds
+libpie-$(CONFIG_PIE)		+= arch/arm/libpie/
+
 # Default target when executing plain make
 ifeq ($(CONFIG_XIP_KERNEL),y)
 KBUILD_IMAGE := xipImage
arch/arm/include/asm/elf.h +1 -0
index 56211f2..a8d036b 100644
@@ -50,6 +50,7 @@ typedef struct user_fp elf_fpregset_t;
 #define R_ARM_NONE		0
 #define R_ARM_PC24		1
 #define R_ARM_ABS32		2
+#define R_ARM_RELATIVE		23
 #define R_ARM_CALL		28
 #define R_ARM_JUMP24		29
 #define R_ARM_V4BX		40
arch/arm/kernel/.gitignore +1 -0
index c5f676c..a055a48 100644
@@ -1 +1,2 @@
 vmlinux.lds
+pie.lds
arch/arm/kernel/Makefile +3 -1
index 86d10dd..652312e 100644
@@ -96,4 +96,6 @@ obj-y				+= psci.o
 obj-$(CONFIG_SMP)		+= psci_smp.o
 endif
 
-extra-y := $(head-y) vmlinux.lds
+obj-$(CONFIG_PIE)		+= pie.o
+
+extra-y := $(head-y) vmlinux.lds pie.lds
arch/arm/kernel/pie.c new file +83 -0

83 lines not on this page · raw message

arch/arm/kernel/pie.lds.S new file +40 -0
new file mode 100644
index 0000000..4fd5ac5
@@ -0,0 +1,40 @@
+/*
+ * ld script to make ARM PIEs
+ * taken from the ARM vmlinux.lds.S version by Russ Dill <[email protected].
+ */
+
+#include <asm-generic/pie.lds.h>
+
+OUTPUT_ARCH(arm)
+
+SECTIONS
+{
+	. = 0x0;
+
+	PIE_COMMON_START
+	.got.plt : {
+		*(.got)
+		*(.got.plt)
+	}
+	.text : {
+		PIE_TEXT_TEXT
+	}
+	PIE_COMMON_END
+
+	PIE_OVERLAY_START
+	OVERLAY : NOCROSSREFS {
+	}
+	PIE_OVERLAY_SEND
+
+	__pie_rel_dyn_start : {
+		VMLINUX_SYMBOL(__pie_rel_dyn_start) = .;
+	}
+	.rel.dyn : {
+		KEEP(*(.rel*))
+	}
+	__pie_rel_dyn_end : {
+		VMLINUX_SYMBOL(__pie_rel_dyn_end) = .;
+	}
+
+	PIE_DISCARDS
+}
arch/arm/kernel/vmlinux.lds.S +2 -0
index 7bcee5c..8c11235 100644
@@ -77,6 +77,8 @@ SECTIONS
 #ifndef CONFIG_SMP_ON_UP
 		*(.alt.smp.init)
 #endif
+		*(.pie.*)
+		*(.ARM.exidx.pie.*.text)
 		*(.discard)
 		*(.discard.*)
 	}
arch/arm/libpie/.gitignore new file +3 -0
new file mode 100644
index 0000000..02e3cd5
@@ -0,0 +1,3 @@
+lib1funcs.S
+ashldi3.S
+string.c
arch/arm/libpie/Makefile new file +32 -0
new file mode 100644
index 0000000..5662e99
@@ -0,0 +1,32 @@
+#
+# linux/arch/arm/libpie/Makefile
+#
+ccflags-y	:= -fpic -mno-single-pic-base -fno-builtin
+
+obj-y		:= empty.o
+obj-y		+= lib1funcs.o ashldi3.o string.o
+
+# string library code (-Os is enforced to keep it much smaller)
+string = $(obj)/string.o
+CFLAGS_string.o := -Os
+
+$(obj)/string.c: $(srctree)/arch/$(SRCARCH)/boot/compressed/string.c
+	$(call cmd,shipped)
+
+# For __aeabi_uidivmod
+lib1funcs = $(obj)/lib1funcs.o
+
+$(obj)/lib1funcs.S: $(srctree)/arch/$(SRCARCH)/lib/lib1funcs.S
+	$(call cmd,shipped)
+
+# For __aeabi_llsl
+ashldi3 = $(obj)/ashldi3.o
+
+$(obj)/ashldi3.S: $(srctree)/arch/$(SRCARCH)/lib/ashldi3.S
+	$(call cmd,shipped)
+
+$(obj)/libpie.o: $(string) $(lib1funcs) $(ashldi3) $(addprefix $(obj)/,$(OBJS))
+	$(call if_changed,ld)
+
+# Make sure files are removed during clean
+extra-y		+= string.c lib1funcs.S ashldi3.S
arch/arm/libpie/empty.S new file +12 -0
new file mode 100644
index 0000000..2416862
@@ -0,0 +1,12 @@
+#include <linux/linkage.h>
+
+ENTRY(__div0)
+ENTRY(__aeabi_unwind_cpp_pr0)
+ENTRY(__aeabi_unwind_cpp_pr1)
+ENTRY(__aeabi_unwind_cpp_pr2)
+	mov	pc, lr
+ENDPROC(__div0)
+ENDPROC(__aeabi_unwind_cpp_pr0)
+ENDPROC(__aeabi_unwind_cpp_pr1)
+ENDPROC(__aeabi_unwind_cpp_pr2)
+
Signature · 1 line
1.8.3.2
author
#8 lore raw

RFC PATCH 7/11 ARM: PIE: Add support for updating PIE relocations

In reply to Russ Dill

This adds support for updating PIE relocations under ARM. This is necessary in the case that the same PIE must run both with virtual mapping (MMU enabled) and physical mapping (MMU disabled).

---
 arch/arm/include/asm/pie.h | 42 +++++++++++++++++++++++++
 arch/arm/kernel/pie.c      |  9 ++++++
 arch/arm/libpie/Makefile   |  2 +-
 arch/arm/libpie/relocate.S | 76 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 128 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm/include/asm/pie.h
 create mode 100644 arch/arm/libpie/relocate.S
arch/arm/include/asm/pie.h new file +42 -0
new file mode 100644
index 0000000..977f11d
@@ -0,0 +1,42 @@
+/*
+ *  arch/arm/include/asm/pie.h
+ *
+ *  Copyright 2013 Texas Instruments, Inc
+ *	Russ Dill <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef _ASMARM_PIE_H
+#define _ASMARM_PIE_H
+
+#include <linux/pie.h>
+
+#ifdef CONFIG_PIE
+extern void __pie_relocate(void);
+extern void __pie___pie_relocate(void);
+
+#define pie_relocate_from_pie()						\
+	__asm__ __volatile__("bl __pie_relocate\n"			\
+	: : : "cc", "memory", "lr", "r4", "r5", "r6", "r7", "r8", "r9");
+
+static inline void pie_relocate_from_kern(struct pie_chunk *chunk)
+{
+	void (*fn)(void) = fn_to_pie(chunk, &__pie___pie_relocate);
+	__asm__ __volatile__("" : : : "cc", "memory", "r4", "r5", "r6",
+				"r7", "r8", "r9");
+	fn();
+}
+#else
+
+#define pie_relocate_from_pie() do {} while(0)
+
+static inline void pie_relocate_from_kern(struct pie_chunk *chunk)
+{
+}
+
+#endif
+
+#endif
arch/arm/kernel/pie.c +9 -0
index 5dff5d6..598562f 100644
@@ -12,10 +12,12 @@
 #include <linux/elf.h>
 
 #include <asm/elf.h>
+#include <asm/pie.h>
 
 extern char __pie_rel_dyn_start[];
 extern char __pie_rel_dyn_end[];
 extern char __pie_tail_offset[];
+extern char __pie_reloc_offset[];
 
 struct arm_pie_tail {
 	int count;
@@ -72,12 +74,19 @@ int pie_arch_fixup(struct pie_chunk *chunk, void *base, void *tail,
 						unsigned long offset)
 {
 	struct arm_pie_tail *pie_tail = tail;
+	void *reloc;
 	int i;
 
 	/* Perform relocation fixups for given offset */
 	for (i = 0; i < pie_tail->count; i++)
 		*((uintptr_t *) (pie_tail->offset[i] + base)) += offset;
 
+	/* Store the PIE offset to tail and recol func */
+	*kern_to_pie(chunk, (uintptr_t *) __pie_tail_offset) = tail - base;
+	reloc = kern_to_pie(chunk,
+				(void *) fnptr_to_addr(&__pie___pie_relocate));
+	*kern_to_pie(chunk, (uintptr_t *) __pie_reloc_offset) = reloc - base;
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(pie_arch_fixup);
arch/arm/libpie/Makefile +1 -1
index 5662e99..b1ac52a 100644
@@ -3,7 +3,7 @@
 #
 ccflags-y	:= -fpic -mno-single-pic-base -fno-builtin
 
-obj-y		:= empty.o
+obj-y		:= relocate.o empty.o
 obj-y		+= lib1funcs.o ashldi3.o string.o
 
 # string library code (-Os is enforced to keep it much smaller)
arch/arm/libpie/relocate.S new file +76 -0

76 lines not on this page · raw message

Signature · 1 line
1.8.3.2
author
#9 lore raw

RFC PATCH 8/11 ARM: PIE: Add macro for generating PIE resume trampoline

In reply to Russ Dill

Add a helper that generates a short snippet of code that updates PIE relocations, loads the stack pointer and calls a C (or asm) function. The code gets placed into a PIE section.

---
 arch/arm/include/asm/suspend.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
arch/arm/include/asm/suspend.h +25 -0
index cd20029..92996f0 100644
@@ -1,6 +1,8 @@
 #ifndef __ASM_ARM_SUSPEND_H
 #define __ASM_ARM_SUSPEND_H
 
+#include <asm/pie.h>
+
 struct sleep_save_sp {
 	u32 *save_ptr_stash;
 	u32 save_ptr_stash_phys;
@@ -9,4 +11,27 @@ struct sleep_save_sp {
 extern void cpu_resume(void);
 extern int cpu_suspend(unsigned long, int (*)(unsigned long));
 
+/**
+ * ARM_PIE_RESUME - generate a PIE trampoline for resume
+ * @proc:	SoC, should match argument used with PIE_OVERLAY_SECTION()
+ * @func:	C or asm function to call at resume
+ * @stack:	stack to use before calling func
+ */
+#define ARM_PIE_RESUME(proc, func, stack)				\
+static void __naked __noreturn __pie(proc) proc##_resume_trampoline2(void) \
+{									\
+	__asm__ __volatile__(						\
+	"	mov	sp, %0\n"					\
+	: : "r"((stack)) : "sp");					\
+									\
+	func();								\
+}									\
+									\
+void __naked __noreturn __pie(proc) proc##_resume_trampoline(void)	\
+{									\
+	pie_relocate_from_pie();					\
+	proc##_resume_trampoline2();					\
+}									\
+EXPORT_PIE_SYMBOL(proc##_resume_trampoline)
+
 #endif
Signature · 1 line
1.8.3.2
author
#10 lore raw

RFC PATCH 9/11 ARM: dts: AM33XX: Associate SRAM with MPU and mark it exec

In reply to Russ Dill

The SRAM is for use by the MPU. Marking it as such makes it easier for PM initialization code to locate the SRAM in order to load a PIE section into it.

Additionally, set the map-exec flag to allow code to be run from SRAM. This is necessary for suspend/resume.

---
 arch/arm/boot/dts/am33xx.dtsi | 2 ++
 1 file changed, 2 insertions(+)
arch/arm/boot/dts/am33xx.dtsi +2 -0
index 302463d..20942a2 100644
@@ -62,6 +62,7 @@
 		mpu {
 			compatible = "ti,omap3-mpu";
 			ti,hwmods = "mpu";
+			sram = <&ocmcram>;
 		};
 	};
 
@@ -507,6 +508,7 @@
 		ocmcram: ocmcram at 40300000 {
 			compatible = "mmio-sram";
 			reg = <0x40300000 0x10000>; /* 64k */
+			map-exec;
 		};
 
 		wkup_m3: wkup_m3 at 44d00000 {
Signature · 1 line
1.8.3.2
author
#11 lore raw

RFC PATCH 10/11 ARM: OMAP2+: AM33XX: Add PIE support for AM33XX

In reply to Russ Dill

This enables CONFIG_PIE for omap2plus_defconfig and adds an am33xx PIE section group. This is necessary for am33xx suspend/resume code as it is written in C.

---
 arch/arm/configs/omap2plus_defconfig | 1 +
 arch/arm/kernel/pie.lds.S            | 1 +
 2 files changed, 2 insertions(+)
arch/arm/configs/omap2plus_defconfig +1 -0
index 5d4c9b8..f342f85 100644
@@ -102,6 +102,7 @@ CONFIG_SENSORS_TSL2550=m
 CONFIG_SENSORS_LIS3_I2C=m
 CONFIG_BMP085_I2C=m
 CONFIG_SRAM=y
+CONFIG_PIE=y
 CONFIG_SCSI=y
 CONFIG_BLK_DEV_SD=y
 CONFIG_SCSI_MULTI_LUN=y
arch/arm/kernel/pie.lds.S +1 -0
index 4fd5ac5..92fba61 100644
@@ -23,6 +23,7 @@ SECTIONS
 
 	PIE_OVERLAY_START
 	OVERLAY : NOCROSSREFS {
+		PIE_OVERLAY_SECTION(am33xx)
 	}
 	PIE_OVERLAY_SEND
 
Signature · 1 line
1.8.3.2
author
#12 lore raw

RFC PATCH 11/11 ARM: OMAP2+: AM33XX: Basic suspend resume support

In reply to Russ Dill

From: Vaibhav Bedia <[email protected]>

AM335x supports various low power modes as documented in section 8.1.4.3 of the AM335x TRM which is available @ http://www.ti.com/litv/pdf/spruh73f

DeepSleep0 mode offers the lowest power mode with limited wakeup sources without a system reboot and is mapped as the suspend state in the kernel. In this state, MPU and PER domains are turned off with the internal RAM held in retention to facilitate resume process. As part of the boot process, the assembly code is copied over to OCMCRAM using the OMAP SRAM code.

AM335x has a Cortex-M3 (WKUP_M3) which assists the MPU in DeepSleep0 entry and exit. WKUP_M3 takes care of the clockdomain and powerdomain transitions based on the intended low power state. MPU needs to load the appropriate WKUP_M3 binary onto the WKUP_M3 memory space before it can leverage any of the PM features like DeepSleep.

The IPC mechanism between MPU and WKUP_M3 uses a mailbox sub-module and 8 IPC registers in the Control module. MPU uses the assigned Mailbox for issuing an interrupt to WKUP_M3 which then goes and checks the IPC registers for the payload. WKUP_M3 has the ability to trigger on interrupt to MPU by executing the "sev" instruction.

In the current implementation when the suspend process is initiated MPU interrupts the WKUP_M3 to let it know about the intent of entering DeepSleep0 and waits for an ACK. When the ACK is received MPU continues with its suspend process to suspend all the drivers and then jumps to assembly in OCMC RAM. The assembly code puts the PLLs in bypass, puts the external RAM in self-refresh mode and then finally execute the WFI instruction. Execution of the WFI instruction triggers another interrupt to the WKUP_M3 which then continues wiht the power down sequence wherein the clockdomain and powerdomain transition takes place. As part of the sleep sequence, WKUP_M3 unmasks the interrupt lines for the wakeup sources. WFI execution on WKUP_M3 causes the hardware to disable the main oscillator of the SoC.

When a wakeup event occurs, WKUP_M3 starts the power-up sequence by switching on the power domains and finally enabling the clock to MPU. Since the MPU gets powered down as part of the sleep sequence in the resume path ROM code starts executing. The ROM code detects a wakeup from sleep and then jumps to the resume location in OCMC which was populated in one of the IPC registers as part of the suspend sequence.

The low level code in OCMC relocks the PLLs, enables access to external RAM and then jumps to the cpu_resume code of the kernel to finish the resume process.

Cc Tony Lingren <[email protected]>
Cc Santosh Shilimkar <[email protected]>
Cc Benoit Cousson <[email protected]>
Cc Paul Walmsley <[email protected]>
Cc Kevin Hilman <[email protected]>
---
 arch/arm/mach-omap2/Kconfig         |   7 +-
 arch/arm/mach-omap2/Makefile        |   2 +
 arch/arm/mach-omap2/board-generic.c |   1 +
 arch/arm/mach-omap2/common.h        |  10 +
 arch/arm/mach-omap2/io.c            |   5 +
 arch/arm/mach-omap2/pm.c            |   3 +-
 arch/arm/mach-omap2/pm33xx.c        | 486 ++++++++++++++++++++++++++++++++++++
 arch/arm/mach-omap2/pm33xx.h        |  68 +++++
 arch/arm/mach-omap2/sleep33xx.c     | 314 +++++++++++++++++++++++
 arch/arm/mach-omap2/wkup_m3.c       | 183 ++++++++++++++
 10 files changed, 1076 insertions(+), 3 deletions(-)
 create mode 100644 arch/arm/mach-omap2/pm33xx.c
 create mode 100644 arch/arm/mach-omap2/pm33xx.h
 create mode 100644 arch/arm/mach-omap2/sleep33xx.c
 create mode 100644 arch/arm/mach-omap2/wkup_m3.c
arch/arm/mach-omap2/Kconfig +5 -2
index 3eed000..ef3fe40 100644
@@ -67,11 +67,14 @@ config SOC_OMAP5
 config SOC_AM33XX
 	bool "AM33XX support"
 	depends on ARCH_MULTI_V7
-	select ARCH_OMAP2PLUS
+	default y
 	select ARM_CPU_SUSPEND if PM
+	select COMMON_CLK
 	select CPU_V7
+	select MAILBOX if PM
 	select MULTI_IRQ_HANDLER
-	select COMMON_CLK
+	select OMAP_MBOX_FWK if PM
+	select OMAP2PLUS_MBOX if PM
 
 config SOC_AM43XX
 	bool "TI AM43x"
arch/arm/mach-omap2/Makefile +2 -0
index d4f6715..42442c4 100644
@@ -87,6 +87,7 @@ obj-$(CONFIG_ARCH_OMAP2)		+= sleep24xx.o
 obj-$(CONFIG_ARCH_OMAP3)		+= pm34xx.o sleep34xx.o
 obj-$(CONFIG_ARCH_OMAP4)		+= pm44xx.o omap-mpuss-lowpower.o
 obj-$(CONFIG_SOC_OMAP5)			+= omap-mpuss-lowpower.o
+obj-$(CONFIG_SOC_AM33XX)		+= pm33xx.o sleep33xx.o wkup_m3.o
 obj-$(CONFIG_PM_DEBUG)			+= pm-debug.o
 
 obj-$(CONFIG_POWER_AVS_OMAP)		+= sr_device.o
@@ -94,6 +95,7 @@ obj-$(CONFIG_POWER_AVS_OMAP_CLASS3)    += smartreflex-class3.o
 
 AFLAGS_sleep24xx.o			:=-Wa,-march=armv6
 AFLAGS_sleep34xx.o			:=-Wa,-march=armv7-a$(plus_sec)
+CFLAGS_sleep33xx.o			:=-march=armv7-a
 
 endif
 
arch/arm/mach-omap2/board-generic.c +1 -0
index aed750c..3f2d6a7 100644
@@ -159,6 +159,7 @@ DT_MACHINE_START(AM33XX_DT, "Generic AM33XX (Flattened Device Tree)")
 	.reserve	= am33xx_reserve,
 	.map_io		= am33xx_map_io,
 	.init_early	= am33xx_init_early,
+	.init_late	= am33xx_init_late,
 	.init_irq	= omap_intc_of_init,
 	.handle_irq	= omap3_intc_handle_irq,
 	.init_machine	= omap_generic_init,
arch/arm/mach-omap2/common.h +10 -0
index 6b8ef74..80bf0da 100644
@@ -69,6 +69,15 @@ static inline int omap4_pm_init(void)
 }
 #endif
 
+#if defined(CONFIG_PM) && defined(CONFIG_SOC_AM33XX)
+int am33xx_pm_init(void);
+#else
+static inline int am33xx_pm_init(void)
+{
+	return 0;
+}
+#endif
+
 #ifdef CONFIG_OMAP_MUX
 int omap_mux_late_init(void);
 #else
@@ -107,6 +116,7 @@ void omap2430_init_late(void);
 void omap3430_init_late(void);
 void omap35xx_init_late(void);
 void omap3630_init_late(void);
+void am33xx_init_late(void);
 void am35xx_init_late(void);
 void ti81xx_init_late(void);
 int omap2_common_pm_late_init(void);
arch/arm/mach-omap2/io.c +5 -0
index 11583a6d..fca216d 100644
@@ -567,6 +567,11 @@ void __init am33xx_init_early(void)
 	omap_hwmod_init_postsetup();
 	omap_clk_init = am33xx_clk_init;
 }
+
+void __init am33xx_init_late(void)
+{
+	am33xx_pm_init();
+}
 #endif
 
 #ifdef CONFIG_SOC_AM43XX
arch/arm/mach-omap2/pm.c +2 -1
index e742118..f8bd883 100644
@@ -305,7 +305,8 @@ int __init omap2_common_pm_late_init(void)
 	}
 
 #ifdef CONFIG_SUSPEND
-	suspend_set_ops(&omap_pm_ops);
+	if (!soc_is_am33xx())
+		suspend_set_ops(&omap_pm_ops);
 #endif
 
 	return 0;
arch/arm/mach-omap2/pm33xx.c new file +486 -0

486 lines not on this page · raw message

arch/arm/mach-omap2/pm33xx.h new file +68 -0

68 lines not on this page · raw message

arch/arm/mach-omap2/sleep33xx.c new file +314 -0

314 lines not on this page · raw message

arch/arm/mach-omap2/wkup_m3.c new file +183 -0

183 lines not on this page · raw message

Signature · 1 line
1.8.3.2
#13 lore raw
In reply to Russ Dill
Quoted 4 lines · Russ Dill

This patch adds support for and demonstrates the usage of an embedded position independent executable (PIE). The goal is to allow the use of C code in situations where carefully written position independent assembly was previously required.

This is perfectly applicable to the ARM TCM memory as well.

Currently we have arch/arm/kernel/tcm.c and related extensions to kernel/vmlinux.kds.S, which enables us to tag some certain code to be compiled into TCM memory which we map statically to 0xfffe0000 thru 0xfffeffff, but this is a better approach, especially nice since it is the solution to the multiplatform situation, as we need to select to copy code into that memory only on the specific target.

I'll try to have a deeper look at this, please keep me on CC for this series.

Yours,
Linus Walleij

#14 lore raw
In reply to Russ Dill

Am Dienstag, 17. September 2013, 14:43:26 schrieb Russ Dill:

Quoted 4 lines

This patch adds support for and demonstrates the usage of an embedded position independent executable (PIE). The goal is to allow the use of C code in situations where carefully written position independent assembly was previously required.

As suggested yesterday evening by Kevin Hilman, just adding my 2ct of support.

This series looks exactly like the foundation I'll need at some point in the (probably still distant) future to handle suspend on my Rockchip platform - where like in your example stuff like putting the ram into selfrefresh has to be done by the os.

Thanks for the work on this
Heiko

#15 lore raw

Hi Russ,

Am Donnerstag, 24. Oktober 2013, 10:09:12 schrieb Heiko Stübner:

Quoted 6 lines

Am Dienstag, 17. September 2013, 14:43:26 schrieb Russ Dill:

Quoted 4 lines

This patch adds support for and demonstrates the usage of an embedded position independent executable (PIE). The goal is to allow the use of C code in situations where carefully written position independent assembly was previously required.

Quoted 7 lines

As suggested yesterday evening by Kevin Hilman, just adding my 2ct of support.

This series looks exactly like the foundation I'll need at some point in the (probably still distant) future to handle suspend on my Rockchip platform - where like in your example stuff like putting the ram into selfrefresh has to be done by the os.

just as it came up recently again for me and I couldn't find any newer version, ist this series still on the table?

Thanks
Heiko

#16 lore raw

Hi Ru

Quoted 12 lines · Heiko St?bner

Hi Russ,

Am Donnerstag, 24. Oktober 2013, 10:09:12 schrieb Heiko St?bner:

Quoted 9 lines

Am Dienstag, 17. September 2013, 14:43:26 schrieb Russ Dill:

Quoted 4 lines

This patch adds support for and demonstrates the usage of an embedded position independent executable (PIE). The goal is to allow the use of C code in situations where carefully written position independent assembly was previously required.

As suggested yesterday evening by Kevin Hilman, just adding my 2ct of support.

Quoted 7 lines
Quoted 4 lines

This series looks exactly like the foundation I'll need at some point in the (probably still distant) future to handle suspend on my Rockchip platform - where like in your example stuff like putting the ram into selfrefresh has to be done by the os.

just as it came up recently again for me and I couldn't find any newer version, ist this series still on the table?

We have to maintain quite some patches, to keep pm working for our project, so we're interested to get this mainline, too. Is someone taking care of these issues? Or is there something i can do to help getting this done?

regards
pascal

#17 lore raw
In reply to Russ Dill
Quoted 4 lines · Russ Dill

This patch adds support for and demonstrates the usage of an embedded position independent executable (PIE). The goal is to allow the use of C code in situations where carefully written position independent assembly was previously required.

Hmm, I thought I had already replied to this but it appears that I haven't.

I really like this approach and think it's a major improvement on what we're already doing on OMAP and a few other ARM platforms. This approach is done in a way that's significantly easier to maintain and extend IMO.

As a next step, I think you should drop the RFC and split it up into a few series that can go via the right trees:

- asm-generic and lib/devres (patches 1, 2, 4)
- drivers/sram (patch 3)
- generic PIE: (patch 5)
- core PIE: (patch 5-8)
- AM33xx support (patch 9-11)

Kevin

#18 lore raw
In reply to Russ Dill
Quoted 11 lines · Russ Dill

This patch adds support for and demonstrates the usage of an embedded position independent executable (PIE). The goal is to allow the use of C code in situations where carefully written position independent assembly was previously required.

The example used is the suspend/resume code for the am335x, a Texas Instruments ARM SoC. In order to save the maximum amount of power during suspend, the am335x has to perform several power saving operations after SDRAM has been disabled, and undo those steps at resume time. The am335x provides an SRAM region for the processor to execute such code.

Quoted 3 lines

A PIE executable was chosen because it limits the types of relocations that must be performed before the code is executed. In the case of ARM, the only required relocation type is a relative relocation of pointers.

I meant to review this series ages ago -- so, apologies for the delay.

Overall, this looks like a pretty nice framework to have, and a fair amount of effort has clearly been put into it.

However, if this is to be a general framework I think it would be good to step back a bit, to understand how well the framework fits, and how much needs to be invented.

Some points that occur to me are as follows -- it would be good to get some discussion of these.

* Assuming the same definitions of libpie functions work in all PIE scenarios may be unsafe.

For example, because ARM's lib1funcs are only used after the MMU is turned on, there is no reason for code in there not to rely on this. The string functions could assume that unaligned accesses will not fault, for example. (I don't know if this is true -- but it's a risk.)

We also need to watch out for position-dependence creeping in if we import code from the main kernel. I'm assuming that can mostly be detected at link-time, though.

* Pasting the same libpie into every PIE will tend to waste space.

Particularly in multiplatform kernels, each PIE my only want a small subset of the libpie content.

Using prewritten code like lib1funcs in libpie may also suffer directly from large code size, because these implementations are all optimised for speed: for example, arch/arm's memset is pretty unrolled, many times larger, and probably not that much faster than a simple hand-written alternative that may run with the MMU off anyway.

* The kernel already has a static relocator for every arch, in the module loader. Do we need another one?

If we compile and link PIEs statically instead, that may be both more efficient and more compact (particularly with tricks like -ffunction-sections -fdata-sections -Wl,--gc-sections etc.) Modules are statically linked, so the module loader should implement nearly all of the necessary heavy lifting already.

Just-in-time relocation doesn't work well with this approach, since the relocations become a lot more complex, but if the relocation can all be done ahead of time it should work well.

I suspect that it may be possible to shrink the am33xx PIE code enough using these approaches that we could simply fix up two copies of it in the SRAM: one for MMU-on and one for MMU-off. Since those two situations involve different code paths, it might also be possible to discard some extra code from each, but I've not investigated. Maybe I'm too optimistic.

* Repeatedly relocating single a PIE between two address spaces is only going to be feasible / necessary in certain situations, so we shouldn't assume that all PIEs are going to work this way.

If the affected code needs to run in an SMP environment, it may be better to fix up the same PIE twice instead, one for each address space -- otherwise you would need locking which would introduce unnecessary inefficiency.

This probably doesn't apply to the OMAP-like case where the DRAM is being configured (there had better not be other CPUs running anyway) -- but could apply to plenty of other scenarios connected with low-level suepend/resume code on recent platforms.

* We should try to avoid having to maintain lists of things in a file somewhere, because that leads to merge headaches.

Instead of hacking linker scripts to describe every PIE, would it be better to have a common build rule for embedding PIEs which just wraps the PIE data in a .o file with appropriate symbols, section annotations, and table entries as appropriate? The global linker script just needs to slurp those sections somewhere appropriate.

If each PIE is a blob of data, we should be able to link PIEs into modules just as easily as linking them into vmlinux, which might be valuable in the future for large multiplatform kernels.

Cheers
---Dave

Quoted 210 lines

The kernel is provided symbols into the PIE executable by way of exporting those symbols from the PIE and then importing them into vmlinux at final link time. Because the PIE is loaded dynamically at runtime, any access to PIE functions or data must pass through special accessor macros that apply the necessary offset. Code within the PIE does not have access to symbols outside of the PIE, but it still can access code and data outside the PIE so long as it is passed pointers to that code/data.

The PIE executable is provided its own set of functions required by gcc, such as memcpy, memmove, etc. The different PIE sections are collected together in the linker script as an overlay so that the kernel only needs one copy of these functions. When the PIE is loaded, the library functions and appropriate sections are copied into a genalloc pool (in the case of the example, backed by SRAM). The PIE code then applies the necessary relocations to the loaded code. Because the relocations are just relative offsets to pointers, the relocations can be reapplied to allow the code to run with the MMU enabled or disabled.

This patchset is a complete rethinking of an earlier patchset [1]. Ard Biesheuvel provided the suggestion to use the PIE executable format for storing the relocatable code within the kernel. Russell King and Dave Martin pointed out the shortcomings of my initial naive approach.

This patchset depends on Rajendra Nayak's SRAM DT-ification patch series [2], Suman Anna's am335x mailbox series [3], and a portion of Dave Gerlach's am335x suspend/resume patchset [4]. I've collected together the necessary dependances and applied this patch series on top of them here [5].

Because special ioremap variants are required on ARM for io mappings that allow code execution, the first two patches provide generic accessors for those variants. The third patch provides a DT and pdata method for instructing misc/sram to map the memory in such a way that allows code execution.

The 4th patch provides a generic set of functions for handling function pointers as addresses and vice versa. This is necessary on ARM because of the way that Thumb2 function pointers are handled by gcc. The PIE framework requires this functionality because it performs translations of function pointers.

The 5th patch is the general PIE framework. The 6th patch is the addition of ARM support for PIE. The 7th patch provides the ability of ARM to fixup PIE code on the fly. This is necessary since at suspend time the MMU will be working, but at resume time, it will be off. The 8th patch provides a predefined trampoline that utilizes the on the fly fixup.

The 9th patch configures the SRAM DT entries for am335x so that they can be easily found by the PM code, and so that they are mapped with exec enabled. The 10th patch adds PIE entries for am335x, and the 11th patch finally adds suspend/resume support for am33xx utilizing C code for suspend/resume paths.

[1] http://www.spinics.net/lists/arm-kernel/msg271525.html
[2] http://comments.gmane.org/gmane.linux.ports.arm.omap/103774
[3] http://www.spinics.net/lists/devicetree/msg00227.html
[4] http://www.spinics.net/lists/linux-omap/msg95305.html
[5] https://github.com/russdill/linux/commits/sram

Russ Dill (10):
asm-generic: io: Add exec versions of ioremap
lib: devres: Add exec versions of devm_ioremap_resource and friends
misc: SRAM: Add option to map SRAM to allow code execution
asm-generic: fncpy: Add function copying macros
PIE: Support embedding position independent executables
ARM: PIE: Add position independent executable embedding to ARM
ARM: PIE: Add support for updating PIE relocations
ARM: PIE: Add macro for generating PIE resume trampoline
ARM: dts: AM33XX: Associate SRAM with MPU and mark it exec
ARM: OMAP2+: AM33XX: Add PIE support for AM33XX

Vaibhav Bedia (1):
ARM: OMAP2+: AM33XX: Basic suspend resume support

 Documentation/devicetree/bindings/misc/sram.txt |   4 +
 Documentation/pie.txt                           | 167 ++++++++
 Makefile                                        |  17 +-
 arch/alpha/include/asm/fncpy.h                  |   1 +
 arch/arc/include/asm/fncpy.h                    |   1 +
 arch/arm/Kconfig                                |   1 +
 arch/arm/Makefile                               |   5 +
 arch/arm/boot/dts/am33xx.dtsi                   |   2 +
 arch/arm/configs/omap2plus_defconfig            |   1 +
 arch/arm/include/asm/elf.h                      |   1 +
 arch/arm/include/asm/fncpy.h                    |  76 +---
 arch/arm/include/asm/io.h                       |   2 +
 arch/arm/include/asm/pie.h                      |  42 ++
 arch/arm/include/asm/suspend.h                  |  25 ++
 arch/arm/kernel/.gitignore                      |   1 +
 arch/arm/kernel/Makefile                        |   4 +-
 arch/arm/kernel/pie.c                           |  92 +++++
 arch/arm/kernel/pie.lds.S                       |  41 ++
 arch/arm/kernel/vmlinux.lds.S                   |   2 +
 arch/arm/libpie/.gitignore                      |   3 +
 arch/arm/libpie/Makefile                        |  32 ++
 arch/arm/libpie/empty.S                         |  12 +
 arch/arm/libpie/relocate.S                      |  76 ++++
 arch/arm/mach-omap2/Kconfig                     |   7 +-
 arch/arm/mach-omap2/Makefile                    |   2 +
 arch/arm/mach-omap2/board-generic.c             |   1 +
 arch/arm/mach-omap2/common.h                    |  10 +
 arch/arm/mach-omap2/io.c                        |   5 +
 arch/arm/mach-omap2/pm.c                        |   3 +-
 arch/arm/mach-omap2/pm33xx.c                    | 486 ++++++++++++++++++++++++
 arch/arm/mach-omap2/pm33xx.h                    |  68 ++++
 arch/arm/mach-omap2/sleep33xx.c                 | 314 +++++++++++++++
 arch/arm/mach-omap2/wkup_m3.c                   | 183 +++++++++
 arch/arm/plat-omap/sram.c                       |   2 +-
 arch/arm64/include/asm/fncpy.h                  |   1 +
 arch/avr32/include/asm/fncpy.h                  |   1 +
 arch/blackfin/include/asm/fncpy.h               |   1 +
 arch/c6x/include/asm/fncpy.h                    |   1 +
 arch/cris/include/asm/fncpy.h                   |   1 +
 arch/frv/include/asm/fncpy.h                    |   1 +
 arch/h8300/include/asm/fncpy.h                  |   1 +
 arch/hexagon/include/asm/fncpy.h                |   1 +
 arch/ia64/include/asm/fncpy.h                   |   1 +
 arch/m32r/include/asm/fncpy.h                   |   1 +
 arch/m68k/include/asm/fncpy.h                   |   1 +
 arch/metag/include/asm/fncpy.h                  |   1 +
 arch/microblaze/include/asm/fncpy.h             |   1 +
 arch/mips/include/asm/fncpy.h                   |   1 +
 arch/mn10300/include/asm/fncpy.h                |   1 +
 arch/openrisc/include/asm/fncpy.h               |   1 +
 arch/parisc/include/asm/fncpy.h                 |   1 +
 arch/powerpc/include/asm/fncpy.h                |   1 +
 arch/s390/include/asm/fncpy.h                   |   1 +
 arch/score/include/asm/fncpy.h                  |   1 +
 arch/sh/include/asm/fncpy.h                     |   1 +
 arch/sparc/include/asm/fncpy.h                  |   1 +
 arch/tile/include/asm/fncpy.h                   |   1 +
 arch/um/include/asm/fncpy.h                     |   1 +
 arch/unicore32/include/asm/fncpy.h              |   1 +
 arch/x86/include/asm/fncpy.h                    |   1 +
 arch/xtensa/include/asm/fncpy.h                 |   1 +
 drivers/misc/sram.c                             |  13 +-
 include/asm-generic/fncpy.h                     | 104 +++++
 include/asm-generic/iomap.h                     |   5 +
 include/asm-generic/pie.lds.h                   |  82 ++++
 include/asm-generic/vmlinux.lds.h               |   1 +
 include/linux/device.h                          |  17 +-
 include/linux/io.h                              |   4 +
 include/linux/pie.h                             | 196 ++++++++++
 include/linux/platform_data/sram.h              |   8 +
 lib/Kconfig                                     |  14 +
 lib/Makefile                                    |   2 +
 lib/devres.c                                    |  97 ++++-
 lib/pie.c                                       | 138 +++++++
 pie/.gitignore                                  |   3 +
 pie/Makefile                                    |  85 +++++
 scripts/link-vmlinux.sh                         |  11 +-
 77 files changed, 2425 insertions(+), 71 deletions(-)
 create mode 100644 Documentation/pie.txt
 create mode 100644 arch/alpha/include/asm/fncpy.h
 create mode 100644 arch/arc/include/asm/fncpy.h
 create mode 100644 arch/arm/include/asm/pie.h
 create mode 100644 arch/arm/kernel/pie.c
 create mode 100644 arch/arm/kernel/pie.lds.S
 create mode 100644 arch/arm/libpie/.gitignore
 create mode 100644 arch/arm/libpie/Makefile
 create mode 100644 arch/arm/libpie/empty.S
 create mode 100644 arch/arm/libpie/relocate.S
 create mode 100644 arch/arm/mach-omap2/pm33xx.c
 create mode 100644 arch/arm/mach-omap2/pm33xx.h
 create mode 100644 arch/arm/mach-omap2/sleep33xx.c
 create mode 100644 arch/arm/mach-omap2/wkup_m3.c
 create mode 100644 arch/arm64/include/asm/fncpy.h
 create mode 100644 arch/avr32/include/asm/fncpy.h
 create mode 100644 arch/blackfin/include/asm/fncpy.h
 create mode 100644 arch/c6x/include/asm/fncpy.h
 create mode 100644 arch/cris/include/asm/fncpy.h
 create mode 100644 arch/frv/include/asm/fncpy.h
 create mode 100644 arch/h8300/include/asm/fncpy.h
 create mode 100644 arch/hexagon/include/asm/fncpy.h
 create mode 100644 arch/ia64/include/asm/fncpy.h
 create mode 100644 arch/m32r/include/asm/fncpy.h
 create mode 100644 arch/m68k/include/asm/fncpy.h
 create mode 100644 arch/metag/include/asm/fncpy.h
 create mode 100644 arch/microblaze/include/asm/fncpy.h
 create mode 100644 arch/mips/include/asm/fncpy.h
 create mode 100644 arch/mn10300/include/asm/fncpy.h
 create mode 100644 arch/openrisc/include/asm/fncpy.h
 create mode 100644 arch/parisc/include/asm/fncpy.h
 create mode 100644 arch/powerpc/include/asm/fncpy.h
 create mode 100644 arch/s390/include/asm/fncpy.h
 create mode 100644 arch/score/include/asm/fncpy.h
 create mode 100644 arch/sh/include/asm/fncpy.h
 create mode 100644 arch/sparc/include/asm/fncpy.h
 create mode 100644 arch/tile/include/asm/fncpy.h
 create mode 100644 arch/um/include/asm/fncpy.h
 create mode 100644 arch/unicore32/include/asm/fncpy.h
 create mode 100644 arch/x86/include/asm/fncpy.h
 create mode 100644 arch/xtensa/include/asm/fncpy.h
 create mode 100644 include/asm-generic/fncpy.h
 create mode 100644 include/asm-generic/pie.lds.h
 create mode 100644 include/linux/pie.h
 create mode 100644 include/linux/platform_data/sram.h
 create mode 100644 lib/pie.c
 create mode 100644 pie/.gitignore
 create mode 100644 pie/Makefile
Signature · 7 lines
1.8.3.2


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel