Skip to contentSkip to conversation

xen/arm32: Introduce alternative runtime patching

PATCH Superseded Wei Chen 2 participants 3 messages Mar 30, 2017 – Mar 31, 2017

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Wei Chen

author
#0 lore raw

PATCH v3 1/1 xen/arm32: Introduce alternative runtime patching

This patch is based on the implementation of ARM64, it introduces alternative runtime patching to ARM32. This allows to patch assembly instruction at runtime to either fix hardware bugs or optimize for certain hardware features on ARM32 platform.

Xen hypervisor is using ARM execution state only on ARM32 platform, Thumb is not used. So, the Thumb only branch instructions (CBZ, CBNZ, TBB and TBH) are not considered in alternatives.

The left ARM32 branch instructions are BX, BLX, BL and B. The instruction BX is taking a register in parameter, so we don't need to rewrite it. The instructions BLX, BL and B are using the similar encoding for the offset and will avoid specific case when extracting and updating the offset.

In this patch, we include alternative.h header file to livepatch.c directly for ARM32 compilation issues. When the alternative patching config is enabled, the livepatch.c will use the alternative functions. In this case, we should include the alternative header file to this file. But for ARM64, it does not include this header file directly. It includes this header file indirectly through:
sched.h->domain.h->page.h->alternative.h.
But, unfortunately, the page.h of ARM32 doesn't include alternative.h, and we don't have the reason to include it to ARM32 page.h now. So we have to include the alternative.h directly in livepatch.c.

---
v2->v3:
1. Introduce a macro to cache blx.
2. Check the instruction conditional bits in open-code to avoid recognizing blx in the macros of b and bl.
3. Explain in code comment, why we mask the H bit for blx in macro.

---
 xen/arch/arm/Kconfig             |  2 +-
 xen/arch/arm/arm32/Makefile      |  1 +
 xen/arch/arm/arm32/insn.c        | 91 ++++++++++++++++++++++++++++++++++++++++
 xen/common/livepatch.c           |  1 +
 xen/include/asm-arm/arm32/insn.h | 75 +++++++++++++++++++++++++++++++++
 xen/include/asm-arm/insn.h       |  2 +
 6 files changed, 171 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/arm/arm32/insn.c
 create mode 100644 xen/include/asm-arm/arm32/insn.h
xen/arch/arm/Kconfig +1 -1
index 2e023d1..43123e6 100644
@@ -12,11 +12,11 @@ config ARM_32
 config ARM_64
 	def_bool y
 	depends on 64BIT
-	select HAS_ALTERNATIVE
 	select HAS_GICV3
 
 config ARM
 	def_bool y
+	select HAS_ALTERNATIVE
 	select HAS_ARM_HDLCD
 	select HAS_DEVICE_TREE
 	select HAS_MEM_ACCESS
xen/arch/arm/arm32/Makefile +1 -0
index 4395693..0ac254f 100644
@@ -4,6 +4,7 @@ obj-$(EARLY_PRINTK) += debug.o
 obj-y += domctl.o
 obj-y += domain.o
 obj-y += entry.o
+obj-y += insn.o
 obj-$(CONFIG_LIVEPATCH) += livepatch.o
 obj-y += proc-v7.o proc-caxx.o
 obj-y += smpboot.o
xen/arch/arm/arm32/insn.c new file +91 -0

91 lines not on this page · raw message

xen/common/livepatch.c +1 -0
index 246e673..f14bcbc 100644
@@ -25,6 +25,7 @@
 #include <xen/livepatch.h>
 #include <xen/livepatch_payload.h>
 
+#include <asm/alternative.h>
 #include <asm/event.h>
 
 /*
xen/include/asm-arm/arm32/insn.h new file +75 -0

75 lines not on this page · raw message

xen/include/asm-arm/insn.h +2 -0
index a205ceb..3489179 100644
@@ -5,6 +5,8 @@
 
 #if defined(CONFIG_ARM_64)
 # include <asm/arm64/insn.h>
+#elif defined(CONFIG_ARM_32)
+# include <asm/arm32/insn.h>
 #else
 # error "unknown ARM variant"
 #endif
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw

Hi Wei,

Quoted 8 lines · Wei Chen
xen/include/asm-arm/arm32/insn.h new file +8 -0
+/* Unconditional branch instructions */
+/*
+ * From ARM DDI 0406C.c Section A8.8.25. We can see blx has a H bit.
+ * In an ARM/Thumb instructions mixed running environment, this bit
+ * can be 1 or 0. Although Xen is only using the ARM instructions
+ * and the H bit is always 0. We mask this bit to catch both of these
+ * two encodings for future-proof.
+ */

xen/include/asm-arm/arm32/insn.h line 50

Quoted 10 lines
xen/include/asm-arm/arm32/insn.h new file +10 -0
+__AARCH32_INSN_FUNCS(blx, 0x0E000000, 0x0A000000)
+
+int32_t aarch32_get_branch_offset(uint32_t insn);
+uint32_t aarch32_set_branch_offset(uint32_t insn, int32_t offset);
+
+/* Wrapper for common code */
+static inline bool insn_is_branch_imm(uint32_t insn)
+{
+    /* Check conditional branch instructions */
+    if ( __CONDITIONAL_INSN(insn) )

This is not what I asked. Whilst I am happy to see a different solution this one is completely wrong. b and bl instruction cannot be unconditional and a user of the helpers aarch32_insn_is_{b,bl} would expect them to do the right things.

By "open-coding" I meant dropping the macro __AARCH32_INS_FUNCS and directly implement insn_is_branch_imm with:

return ( (insn & 0x0E000000) == 0x0A000000 );

With the comment on top explain the check.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#2 lore raw

Hi Julien,

Quoted 27 lines · Julien Grall

Hi Wei,

Quoted 8 lines · Wei Chen
xen/include/asm-arm/arm32/insn.h new file +8 -0
+/* Unconditional branch instructions */
+/*
+ * From ARM DDI 0406C.c Section A8.8.25. We can see blx has a H bit.
+ * In an ARM/Thumb instructions mixed running environment, this bit
+ * can be 1 or 0. Although Xen is only using the ARM instructions
+ * and the H bit is always 0. We mask this bit to catch both of these
+ * two encodings for future-proof.
+ */

xen/include/asm-arm/arm32/insn.h line 50

Quoted 10 lines
xen/include/asm-arm/arm32/insn.h new file +10 -0
+__AARCH32_INSN_FUNCS(blx, 0x0E000000, 0x0A000000)
+
+int32_t aarch32_get_branch_offset(uint32_t insn);
+uint32_t aarch32_set_branch_offset(uint32_t insn, int32_t offset);
+
+/* Wrapper for common code */
+static inline bool insn_is_branch_imm(uint32_t insn)
+{
+    /* Check conditional branch instructions */
+    if ( __CONDITIONAL_INSN(insn) )

This is not what I asked. Whilst I am happy to see a different solution this one is completely wrong. b and bl instruction cannot be unconditional and a user of the helpers aarch32_insn_is_{b,bl} would expect them to do the right things.

Quoted 7 lines

By "open-coding" I meant dropping the macro __AARCH32_INS_FUNCS and directly implement insn_is_branch_imm with:

return ( (insn & 0x0E000000) == 0x0A000000 );

With the comment on top explain the check.

Thanks. This will make the code more clear. I would add comment to code to explain this check.

Quoted 2 lines

Cheers,

Signature · 7 lines
Regards,
Wei Chen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel