Skip to contentSkip to conversation

xen/arm32: Introduce alternative runtime patching

PATCH Superseded Wei Chen 2 participants 5 messages Mar 28, 2017 – Mar 30, 2017

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Wei Chen

author
#0 lore raw

PATCH v2 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.

---
v1->v2:
1. Remove duplicate header file.
2. Remove pointless offset check and warning message.
3. Add Reviewed-by tag from Konrad Rzeszutek Wilk.
4. Documented about why we can check b and blx at the same time.
5. Update the macro of b to cover blx.
6. Update the referred ARM ARM document version.

---
 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 | 65 ++++++++++++++++++++++++++++
 xen/include/asm-arm/insn.h       |  2 +
 6 files changed, 161 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 +65 -0

65 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 30 lines · Wei Chen
xen/include/asm-arm/arm32/insn.h new file +24 -0
new file mode 100644
index 0000000..4cda69e
@@ -0,0 +1,65 @@
+/*
+  * Copyright (C) 2017 ARM Ltd.
+  *
+  * 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.
+  *
+  * This program is distributed in the hope that it will be useful,
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  * GNU General Public License for more details.
+  *
+  * You should have received a copy of the GNU General Public License
+  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+  */
+#ifndef __ARCH_ARM_ARM32_INSN
+#define __ARCH_ARM_ARM32_INSN
+
+#include <xen/types.h>
+
+#define __AARCH32_INSN_FUNCS(abbr, mask, val)   \
+static always_inline bool_t aarch32_insn_is_##abbr(uint32_t code) \
+{                                                                 \
+    return (code & (mask)) == (val);                              \

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

Quoted 10 lines
xen/include/asm-arm/arm32/insn.h new file +10 -0
+}
+
+/*
+ * From ARM DDI 0406C.c Section A8.8.18 and A8.8.25. We can see that
+ * unconditional blx and conditional b have the same value field and imm
+ * length. And from ARM DDI 0406C.c Section A5.7 Table A5-23, we can see
+ * that the blx is the only one unconditional instruction has the same
+ * value with conditional branch instructions. So we define the b and blx
+ * in the same macro to check them at the same time.
+ */

I don't think this is true. The encodings are:
- b cccc1010xxxxxxxxxxxxxxxxxxxxxxxx
- bl cccc1011xxxxxxxxxxxxxxxxxxxxxxxx
- blx 1111101Hxxxxxxxxxxxxxxxxxxxxxxxx

where cccc != 0b1111. So both helpers (aarch32_insn_is_{b_or_blx,bl}) will recognize the blx instruction depending on the value of bit H.

That's why I suggested to introduce a new helper checking for blx.

Quoted 37 lines
xen/include/asm-arm/arm32/insn.h new file +31 -0
+__AARCH32_INSN_FUNCS(b_or_blx,  0x0F000000, 0x0A000000)
+__AARCH32_INSN_FUNCS(bl,        0x0F000000, 0x0B000000)
+
+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)
+{
+    return ( aarch32_insn_is_b_or_blx(insn) || aarch32_insn_is_bl(insn) );
+}
+
+static inline int32_t insn_get_branch_offset(uint32_t insn)
+{
+    return aarch32_get_branch_offset(insn);
+}
+
+static inline uint32_t insn_set_branch_offset(uint32_t insn, int32_t offset)
+{
+    return aarch32_set_branch_offset(insn, offset);
+}
+
+#endif /* !__ARCH_ARM_ARM32_INSN */
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/include/asm-arm/insn.h +0 -0
index a205ceb..3489179 100644

xen/include/asm-arm/insn.h line 12

Quoted 8 lines
xen/include/asm-arm/insn.h +2 -0
@@ -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

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 49 lines · Julien Grall

Hi Wei,

Quoted 30 lines · Wei Chen
xen/include/asm-arm/arm32/insn.h new file +24 -0
new file mode 100644
index 0000000..4cda69e
@@ -0,0 +1,65 @@
+/*
+  * Copyright (C) 2017 ARM Ltd.
+  *
+  * 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.
+  *
+  * This program is distributed in the hope that it will be useful,
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  * GNU General Public License for more details.
+  *
+  * You should have received a copy of the GNU General Public License
+  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+  */
+#ifndef __ARCH_ARM_ARM32_INSN
+#define __ARCH_ARM_ARM32_INSN
+
+#include <xen/types.h>
+
+#define __AARCH32_INSN_FUNCS(abbr, mask, val)   \
+static always_inline bool_t aarch32_insn_is_##abbr(uint32_t code) \
+{                                                                 \
+    return (code & (mask)) == (val);                              \

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

Quoted 10 lines
xen/include/asm-arm/arm32/insn.h new file +10 -0
+}
+
+/*
+ * From ARM DDI 0406C.c Section A8.8.18 and A8.8.25. We can see that
+ * unconditional blx and conditional b have the same value field and imm
+ * length. And from ARM DDI 0406C.c Section A5.7 Table A5-23, we can see
+ * that the blx is the only one unconditional instruction has the same
+ * value with conditional branch instructions. So we define the b and blx
+ * in the same macro to check them at the same time.
+ */

I don't think this is true. The encodings are:
- b cccc1010xxxxxxxxxxxxxxxxxxxxxxxx
- bl cccc1011xxxxxxxxxxxxxxxxxxxxxxxx
- blx 1111101Hxxxxxxxxxxxxxxxxxxxxxxxx

Quoted 3 lines

where cccc != 0b1111. So both helpers (aarch32_insn_is_{b_or_blx,bl}) will recognize the blx instruction depending on the value of bit H.

I think I had made a misunderstanding of the H bit. I always thought the H bit in ARM instruction set is 0.

Quoted 2 lines

That's why I suggested to introduce a new helper checking for blx.

I think that's not enough. Current macro will mask the conditional bits. So no matter what the value of H bit, the blx will be recognized in aarch32_insn_is_{b, bl}.

I think we should update the __AARCH32_INSN_FUNCS to cover the cond bits.

#define __UNCONDITIONAL_INSN(code) (((code) >> 28) == 0xF)

#define __AARCH32_INSN_FUNCS(abbr, mask, val) \
static always_inline bool_t aarch32_insn_is_##abbr(uint32_t code) \

{                                                                 \
     return !__UNCONDITIONAL_INSN(code) && (code & (mask)) == (val);   \
}

#define __AARCH32_UNCOND_INSN_FUNCS(abbr, mask, val) \
static always_inline bool_t aarch32_insn_is_##abbr(uint32_t code) \

{                                                                 \
     return __UNCONDITIONAL_INSN(code) && (code & (mask)) == (val);   \
}

__AARCH32_UNCOND_INSN_FUNCS(blx, 0x0E000000, 0x0A000000)

Quoted 48 lines
Quoted 37 lines
xen/include/asm-arm/arm32/insn.h new file +31 -0
+__AARCH32_INSN_FUNCS(b_or_blx,  0x0F000000, 0x0A000000)
+__AARCH32_INSN_FUNCS(bl,        0x0F000000, 0x0B000000)
+
+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)
+{
+    return ( aarch32_insn_is_b_or_blx(insn) || aarch32_insn_is_bl(insn) );
+}
+
+static inline int32_t insn_get_branch_offset(uint32_t insn)
+{
+    return aarch32_get_branch_offset(insn);
+}
+
+static inline uint32_t insn_set_branch_offset(uint32_t insn, int32_t offset)
+{
+    return aarch32_set_branch_offset(insn, offset);
+}
+
+#endif /* !__ARCH_ARM_ARM32_INSN */
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/include/asm-arm/insn.h +0 -0
index a205ceb..3489179 100644

xen/include/asm-arm/insn.h line 12

Quoted 8 lines
xen/include/asm-arm/insn.h +2 -0
@@ -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

Cheers,

Signature · 7 lines
Regards,
Wei Chen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#3 lore raw
Quoted 52 lines · Wei Chen

Hi Julien,

Quoted 49 lines · Julien Grall

Hi Wei,

Quoted 30 lines · Wei Chen
xen/include/asm-arm/arm32/insn.h new file +24 -0
new file mode 100644
index 0000000..4cda69e
@@ -0,0 +1,65 @@
+/*
+  * Copyright (C) 2017 ARM Ltd.
+  *
+  * 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.
+  *
+  * This program is distributed in the hope that it will be useful,
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  * GNU General Public License for more details.
+  *
+  * You should have received a copy of the GNU General Public License
+  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+  */
+#ifndef __ARCH_ARM_ARM32_INSN
+#define __ARCH_ARM_ARM32_INSN
+
+#include <xen/types.h>
+
+#define __AARCH32_INSN_FUNCS(abbr, mask, val)   \
+static always_inline bool_t aarch32_insn_is_##abbr(uint32_t code) \
+{                                                                 \
+    return (code & (mask)) == (val);                              \

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

Quoted 10 lines
xen/include/asm-arm/arm32/insn.h new file +10 -0
+}
+
+/*
+ * From ARM DDI 0406C.c Section A8.8.18 and A8.8.25. We can see that
+ * unconditional blx and conditional b have the same value field and imm
+ * length. And from ARM DDI 0406C.c Section A5.7 Table A5-23, we can see
+ * that the blx is the only one unconditional instruction has the same
+ * value with conditional branch instructions. So we define the b and blx
+ * in the same macro to check them at the same time.
+ */

I don't think this is true. The encodings are:
- b cccc1010xxxxxxxxxxxxxxxxxxxxxxxx
- bl cccc1011xxxxxxxxxxxxxxxxxxxxxxxx
- blx 1111101Hxxxxxxxxxxxxxxxxxxxxxxxx

Quoted 6 lines
Quoted 3 lines

where cccc != 0b1111. So both helpers (aarch32_insn_is_{b_or_blx,bl}) will recognize the blx instruction depending on the value of bit H.

I think I had made a misunderstanding of the H bit. I always thought the H bit in ARM instruction set is 0.

Because Xen is only using ARM instructions, blx will always have H = 0. But this is not what you described in your comment.

Quoted 19 lines
Quoted 3 lines

That's why I suggested to introduce a new helper checking for blx.

I think that's not enough. Current macro will mask the conditional bits. So no matter what the value of H bit, the blx will be recognized in aarch32_insn_is_{b, bl}.

I think we should update the __AARCH32_INSN_FUNCS to cover the cond bits.

#define __UNCONDITIONAL_INSN(code) (((code) >> 28) == 0xF)

#define __AARCH32_INSN_FUNCS(abbr, mask, val) \
static always_inline bool_t aarch32_insn_is_##abbr(uint32_t code) \

{                                                                 \
     return !__UNCONDITIONAL_INSN(code) && (code & (mask)) == (val);   \
}
Quoted 7 lines

#define __AARCH32_UNCOND_INSN_FUNCS(abbr, mask, val) \
static always_inline bool_t aarch32_insn_is_##abbr(uint32_t code) \

{                                                                 \
     return __UNCONDITIONAL_INSN(code) && (code & (mask)) == (val);   \
}

__AARCH32_UNCOND_INSN_FUNCS(blx, 0x0E000000, 0x0A000000)

Looking at the code you aarch32_insn_is_* helpers are only used in aarch32_insn_is_branch_imm. So why don't you open-code the checks in the latter helper?

Cheers,

Signature · 6 lines
Julien Grall

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

Hi Julien,

Quoted 59 lines · Julien Grall
Quoted 56 lines · Wei Chen

Hi Julien,

Quoted 52 lines · Julien Grall

Hi Wei,

Quoted 30 lines · Wei Chen
xen/include/asm-arm/arm32/insn.h new file +24 -0
new file mode 100644
index 0000000..4cda69e
@@ -0,0 +1,65 @@
+/*
+  * Copyright (C) 2017 ARM Ltd.
+  *
+  * 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.
+  *
+  * This program is distributed in the hope that it will be useful,
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  * GNU General Public License for more details.
+  *
+  * You should have received a copy of the GNU General Public License
+  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+  */
+#ifndef __ARCH_ARM_ARM32_INSN
+#define __ARCH_ARM_ARM32_INSN
+
+#include <xen/types.h>
+
+#define __AARCH32_INSN_FUNCS(abbr, mask, val)   \
+static always_inline bool_t aarch32_insn_is_##abbr(uint32_t code) \
+{                                                                 \
+    return (code & (mask)) == (val);                              \

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

Quoted 10 lines
xen/include/asm-arm/arm32/insn.h new file +10 -0
+}
+
+/*
+ * From ARM DDI 0406C.c Section A8.8.18 and A8.8.25. We can see that
+ * unconditional blx and conditional b have the same value field and imm
+ * length. And from ARM DDI 0406C.c Section A5.7 Table A5-23, we can see
+ * that the blx is the only one unconditional instruction has the same
+ * value with conditional branch instructions. So we define the b and blx
+ * in the same macro to check them at the same time.
+ */

I don't think this is true. The encodings are:
- b cccc1010xxxxxxxxxxxxxxxxxxxxxxxx
- bl cccc1011xxxxxxxxxxxxxxxxxxxxxxxx
- blx 1111101Hxxxxxxxxxxxxxxxxxxxxxxxx

where cccc != 0b1111. So both helpers (aarch32_insn_is_{b_or_blx,bl}) will recognize the blx instruction depending on the value of bit H.

Quoted 5 lines
Quoted 2 lines

I think I had made a misunderstanding of the H bit. I always thought the H bit in ARM instruction set is 0.

Because Xen is only using ARM instructions, blx will always have H = 0. But this is not what you described in your comment.

Yes, I missed that. I would fix it.

Quoted 26 lines
Quoted 26 lines
Quoted 4 lines

That's why I suggested to introduce a new helper checking for blx.

I think that's not enough. Current macro will mask the conditional bits. So no matter what the value of H bit, the blx will be recognized in aarch32_insn_is_{b, bl}.

I think we should update the __AARCH32_INSN_FUNCS to cover the cond bits.

#define __UNCONDITIONAL_INSN(code) (((code) >> 28) == 0xF)

#define __AARCH32_INSN_FUNCS(abbr, mask, val) \
static always_inline bool_t aarch32_insn_is_##abbr(uint32_t code) \

{                                                                 \
     return !__UNCONDITIONAL_INSN(code) && (code & (mask)) == (val);   \
}

#define __AARCH32_UNCOND_INSN_FUNCS(abbr, mask, val) \
static always_inline bool_t aarch32_insn_is_##abbr(uint32_t code) \

{                                                                 \
     return __UNCONDITIONAL_INSN(code) && (code & (mask)) == (val);   \
}
Quoted 6 lines
Quoted 1 line

__AARCH32_UNCOND_INSN_FUNCS(blx, 0x0E000000, 0x0A000000)

Looking at the code you aarch32_insn_is_* helpers are only used in aarch32_insn_is_branch_imm. So why don't you open-code the checks in the latter helper?

That's a good opinion!

Quoted 2 lines

Cheers,

Signature · 7 lines
Regards,
Wei Chen

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