Skip to contentSkip to conversation

xen/arm: Correctly support WARN_ON

PATCH Superseded Julien Grall 2 participants 10 messages Aug 5, 2014 – Sep 10, 2014

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen/arm: Correctly support WARN_ON

Currently the hypervisor will hang if it hits a WARN_ON.

The implemention uses an undefined instruction, made ourself because ARM doesn't provide one, to implement BUG/ASSERT/WARN_ON, and sets up the different tables (one for each type) which contain useful information.

This is based on the x86 implementation (include/asm-x86/bug.h). Unfortunately the structure can't be shared because many ARM{32,64} gcc versions doesn't correctly support %c. The support of executing a function in an exception handler is also keep unimplemented on ARM. Therefore, dump_execution_state is implement as WARN()

The current opcode used to go in exception mode may not be undefined on ARM64. Use the instruction "brk" to generate a software debug exception.

---
Changes in v2:
- Missing static in do_bug_frame prototype
- Add support for ARM64 by using the instruction brk
- Implement dump_execution_state as WARN

---
 xen/arch/arm/arm32/traps.c      |   22 ++++++++-
 xen/arch/arm/traps.c            |  103 +++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/xen.lds.S          |    8 +++
 xen/include/asm-arm/arm32/bug.h |   13 +++++
 xen/include/asm-arm/arm64/bug.h |   10 ++++
 xen/include/asm-arm/bug.h       |   77 +++++++++++++++++++++++++++--
 xen/include/asm-arm/debugger.h  |    2 +-
 xen/include/asm-arm/processor.h |   17 ++++++-
 8 files changed, 246 insertions(+), 6 deletions(-)
 create mode 100644 xen/include/asm-arm/arm32/bug.h
 create mode 100644 xen/include/asm-arm/arm64/bug.h
xen/arch/arm/arm32/traps.c +21 -1
index ff0b945..baf8c41 100644
@@ -18,6 +18,7 @@
 
 #include <xen/config.h>
 #include <xen/lib.h>
+#include <xen/kernel.h>
 
 #include <public/xen.h>
 
@@ -25,7 +26,26 @@
 
 asmlinkage void do_trap_undefined_instruction(struct cpu_user_regs *regs)
 {
-    do_unexpected_trap("Undefined Instruction", regs);
+    uint64_t pc = regs->pc;
+    uint32_t instr;
+
+    if ( !is_kernel_text(pc) &&
+         (system_state >= SYS_STATE_active || !is_kernel_inittext(pc)) )
+        goto die;
+
+    /* PC is always 4-byte align, as Xen is using ARM instruction set */
+    instr = *((uint32_t *)regs->pc);
+    if ( instr != BUG_OPCODE )
+        goto die;
+
+    if ( do_bug_frame(regs, regs->pc) )
+        goto die;
+
+    regs->pc += 4;
+    return;
+
+die:
+    do_unexpected_trap("undefined instruction", regs);
 }
 
 asmlinkage void do_trap_supervisor_call(struct cpu_user_regs *regs)
xen/arch/arm/traps.c +103 -0

121 lines not on this page · raw message

xen/arch/arm/xen.lds.S +8 -0
index 079e085..cca1d8c 100644
@@ -40,6 +40,14 @@ SECTIONS
   . = ALIGN(PAGE_SIZE);
   .rodata : {
         _srodata = .;          /* Read-only data */
+        /* Bug frames table */
+       __start_bug_frames = .;
+       *(.bug_frames.0)
+       __stop_bug_frames_0 = .;
+       *(.bug_frames.1)
+       __stop_bug_frames_1 = .;
+       *(.bug_frames.2)
+       __stop_bug_frames_2 = .;
        *(.rodata)
        *(.rodata.*)
         _erodata = .;          /* End of read-only data */
xen/include/asm-arm/arm32/bug.h new file +13 -0
new file mode 100644
index 0000000..155b420
@@ -0,0 +1,13 @@
+#ifndef __ARM_ARM32_BUG_H__
+#define __ARM_ARM32_BUG_H__
+
+#include <xen/stringify.h>
+
+/* ARMv7 provides a list of undefined opcode (see A8.8.247 DDI 0406C.b)
+ * Use one them encoding A1 to go in exception mode
+ */
+#define BUG_OPCODE  0xe7f00f0
+
+#define BUG_INSTR ".word " __stringify(BUG_OPCODE)
+
+#endif /* __ARM_ARM32_BUG_H__ */
xen/include/asm-arm/arm64/bug.h new file +10 -0
new file mode 100644
index 0000000..42b0e4f
@@ -0,0 +1,10 @@
+#ifndef __ARM_ARM64_BUG_H__
+#define __ARM_ARM64_BUG_H__
+
+#include <xen/stringify.h>
+
+#define BRK_BUG_FRAME 1
+
+#define BUG_INSTR "brk " __stringify(BRK_BUG_FRAME)
+
+#endif /* __ARM_ARM64_BUG_H__ */
xen/include/asm-arm/bug.h +74 -3

84 lines not on this page · raw message

xen/include/asm-arm/debugger.h +1 -1
index 916860b..ac776ef 100644
@@ -1,7 +1,7 @@
 #ifndef __ARM_DEBUGGER_H__
 #define __ARM_DEBUGGER_H__
 
-#define debugger_trap_fatal(v, r) ((void) 0)
+#define debugger_trap_fatal(v, r) (0)
 #define debugger_trap_immediate() ((void) 0)
 
 #endif /* __ARM_DEBUGGER_H__ */
xen/include/asm-arm/processor.h +16 -1
index 9d230f3..acaf566 100644
@@ -3,6 +3,7 @@
 
 #include <asm/cpregs.h>
 #include <asm/sysregs.h>
+#include <public/arch-arm.h>
 
 /* MIDR Main ID Register */
 #define MIDR_MASK    0xff0ffff0
@@ -122,6 +123,9 @@
 #define HSR_EC_INSTR_ABORT_CURR_EL  0x21
 #define HSR_EC_DATA_ABORT_LOWER_EL  0x24
 #define HSR_EC_DATA_ABORT_CURR_EL   0x25
+#ifdef CONFIG_ARM_64
+#define HSR_EC_BRK                  0x3c
+#endif
 
 /* FSR format, common */
 #define FSR_LPAE                (_AC(1,UL)<<9)
@@ -358,6 +362,17 @@ union hsr {
         unsigned long len:1;   /* Instruction length */
         unsigned long ec:6;    /* Exception Class */
     } dabt; /* HSR_EC_DATA_ABORT_* */
+
+#ifdef CONFIG_ARM_64
+    struct hsr_brk {
+        unsigned long comment:16;   /* Comment */
+        unsigned long res0:9;
+        unsigned long len:1;        /* Instruction length */
+        unsigned long ec:6;         /* Exception Class */
+    } brk;
+#endif
+
+
 };
 #endif
 
@@ -487,7 +502,7 @@ void panic_PAR(uint64_t par);
 void show_execution_state(struct cpu_user_regs *regs);
 void show_registers(struct cpu_user_regs *regs);
 //#define dump_execution_state() run_in_exception_handler(show_execution_state)
-#define dump_execution_state() asm volatile (".word 0xe7f000f0\n"); /* XXX */
+#define dump_execution_state() WARN()
 
 #define cpu_relax() barrier() /* Could yield? */
 
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 50 lines · Julien Grall

Currently the hypervisor will hang if it hits a WARN_ON.

The implemention uses an undefined instruction, made ourself because ARM doesn't provide one, to implement BUG/ASSERT/WARN_ON, and sets up the different tables (one for each type) which contain useful information.

This is based on the x86 implementation (include/asm-x86/bug.h). Unfortunately the structure can't be shared because many ARM{32,64} gcc versions doesn't correctly support %c. The support of executing a function in an exception handler is also keep unimplemented on ARM. Therefore, dump_execution_state is implement as WARN()

The current opcode used to go in exception mode may not be undefined on ARM64. Use the instruction "brk" to generate a software debug exception.

---
Changes in v2:
- Missing static in do_bug_frame prototype
- Add support for ARM64 by using the instruction brk
- Implement dump_execution_state as WARN

---
 xen/arch/arm/arm32/traps.c      |   22 ++++++++-
 xen/arch/arm/traps.c            |  103 +++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/xen.lds.S          |    8 +++
 xen/include/asm-arm/arm32/bug.h |   13 +++++
 xen/include/asm-arm/arm64/bug.h |   10 ++++
 xen/include/asm-arm/bug.h       |   77 +++++++++++++++++++++++++++--
 xen/include/asm-arm/debugger.h  |    2 +-
 xen/include/asm-arm/processor.h |   17 ++++++-
 8 files changed, 246 insertions(+), 6 deletions(-)
 create mode 100644 xen/include/asm-arm/arm32/bug.h
 create mode 100644 xen/include/asm-arm/arm64/bug.h
xen/arch/arm/arm32/traps.c +1 -0
index ff0b945..baf8c41 100644
@@ -18,6 +18,7 @@
 
 #include <xen/config.h>
 #include <xen/lib.h>
+#include <xen/kernel.h>
 
 #include <public/xen.h>
 
@@ -25,7 +26,26 @@
 
 asmlinkage void do_trap_undefined_instruction(struct cpu_user_regs *regs)

xen/arch/arm/arm32/traps.c line 36

Quoted 10 lines
xen/arch/arm/arm32/traps.c +8 -1
 {
-    do_unexpected_trap("Undefined Instruction", regs);
+    uint64_t pc = regs->pc;
+    uint32_t instr;
+
+    if ( !is_kernel_text(pc) &&
+         (system_state >= SYS_STATE_active || !is_kernel_inittext(pc)) )
+        goto die;
+
+    /* PC is always 4-byte align, as Xen is using ARM instruction set */

"aligned"

Is it worth a check here? I presume the nested fault if PC were misaligned would be pretty exciting, print+goto die would seem appropriate.

xen/arch/arm/arm32/traps.c line 48

Quoted 12 lines
xen/arch/arm/arm32/traps.c +12 -0
+    instr = *((uint32_t *)regs->pc);
+    if ( instr != BUG_OPCODE )
+        goto die;
+
+    if ( do_bug_frame(regs, regs->pc) )
+        goto die;
+
+    regs->pc += 4;
+    return;
+
+die:
+    do_unexpected_trap("undefined instruction", regs);

No need to change the case of this message.

xen/arch/arm/arm32/traps.c

Quoted 6 lines
xen/arch/arm/arm32/traps.c +5 -0
 }> +#ifdef CONFIG_ARM_64
+static void do_trap_brk(struct cpu_user_regs *regs, union hsr hsr)
+{
+    /* HCR_EL2.TGE and MDCR_EL2.TDE are not set so we never receive
+     * software breakpoint exception for EL1 and EL0 here
+     */

BUG_ON? ;-O

xen/arch/arm/arm32/traps.c

Quoted 2 lines
xen/arch/arm/arm32/traps.c +2 -0
+die:
+        do_unexpected_trap("undefined breakpoint value", regs);

Please can you capitilise this to match the other callers.

Quoted 1 line

+/* Many version of GCC doesn't support the asm %c parameter which would

"versions".

Quoted 3 lines

+ * be preferable to this unpleasantness. We use mergeable string
+ * sections to avoid multiple copies of the string appearing in the
+ * Xen image.

OOI what is the size increase of the final (stripped) binary with this patch?

Ian.

#2 lore raw

Hi Ian,

Quoted 3 lines · Ian Campbell
Quoted 1 line

+ /* PC is always 4-byte align, as Xen is using ARM instruction set */

"aligned"

Ok.

Quoted 3 lines

Is it worth a check here? I presume the nested fault if PC were misaligned would be pretty exciting, print+goto die would seem appropriate.

I don't think so. The "undefined instruction" is only used when the processor is unable to decode the instruction. If it fails to load the instruction (because a wrong memory address, pc misaligned), Xen would have received a "prefetch abort".

Quoted 6 lines
Quoted 6 lines
xen/arch/arm/arm32/traps.c +6 -0
+    instr = *((uint32_t *)regs->pc);
+    if ( instr != BUG_OPCODE )
+        goto die;
+
+    if ( do_bug_frame(regs, regs->pc) )
+        goto die;
Quoted 8 lines

xen/arch/arm/arm32/traps.c line 48

Quoted 6 lines
xen/arch/arm/arm32/traps.c +6 -0
+
+    regs->pc += 4;
+    return;
+
+die:
+    do_unexpected_trap("undefined instruction", regs);

No need to change the case of this message.

Ok.

Quoted 8 lines

xen/arch/arm/traps.c void do_unexpected_trap(const char *msg, struct cpu_user_regs *regs) · line 1082

Quoted 6 lines
  }> +#ifdef CONFIG_ARM_64
xen/arch/arm/traps.c +5 -0
+static void do_trap_brk(struct cpu_user_regs *regs, union hsr hsr)
+{
+    /* HCR_EL2.TGE and MDCR_EL2.TDE are not set so we never receive
+     * software breakpoint exception for EL1 and EL0 here
+     */

BUG_ON? ;-O

Sounds a good plan.

Quoted 4 lines

xen/arch/arm/traps.c void do_unexpected_trap(const char *msg, struct cpu_user_regs *regs) · line 1096

Quoted 2 lines
xen/arch/arm/traps.c +2 -0
+die:
+        do_unexpected_trap("undefined breakpoint value", regs);

Please can you capitilise this to match the other callers.

Will do.

Quoted 10 lines
Quoted 2 lines

+/* Many version of GCC doesn't support the asm %c parameter which would

"versions".

Quoted 3 lines

+ * be preferable to this unpleasantness. We use mergeable string
+ * sections to avoid multiple copies of the string appearing in the
+ * Xen image.

OOI what is the size increase of the final (stripped) binary with this patch?

I compared the file xen/xen and on both arm32/arm64 the binary is smaller of about 2%.

I think this is because previously gcc wasn't merge the string when BUG_ON was used in inline function.

Regards,

Signature · 1 line
Julien Grall
#3 lore raw
Quoted 9 lines · Julien Grall
Quoted 3 lines

OOI what is the size increase of the final (stripped) binary with this patch?

I compared the file xen/xen and on both arm32/arm64 the binary is smaller of about 2%.

I think this is because previously gcc wasn't merge the string when BUG_ON was used in inline function.

BTW this series won't work correctly because of XSA-102 on ARM64 when WARN_ON/BUG_ON/ASSERT is used in startup code. This is because the "current" is set to a debug sanity value.

Do you plan to send the series to add support for 32-bit userspace on 64-bits kernels for Xen 4.5?

Regards,

Signature · 1 line
Julien Grall
#4 lore raw
Quoted 11 lines · Julien Grall
Quoted 9 lines · Julien Grall
Quoted 3 lines

OOI what is the size increase of the final (stripped) binary with this patch?

I compared the file xen/xen and on both arm32/arm64 the binary is smaller of about 2%.

I think this is because previously gcc wasn't merge the string when BUG_ON was used in inline function.

Quoted 6 lines

BTW this series won't work correctly because of XSA-102 on ARM64 when WARN_ON/BUG_ON/ASSERT is used in startup code.
This is because the "current" is set to a debug sanity value.

Do you plan to send the series to add support for 32-bit userspace on 64-bits kernels for Xen 4.5?

I must confess I'd forgotten about it. I'll dig it out and brush the cobwebs off ASAP.

In the meantime can the test at the head of do_trap_hypervisor be extended to DTRT with traps from EL2 (i.e. skip the rest of the expression)?

Ian.

#5 lore raw

Hi Ian,

Quoted 16 lines · Ian Campbell
Quoted 15 lines · Julien Grall
Quoted 9 lines · Julien Grall
Quoted 3 lines

OOI what is the size increase of the final (stripped) binary with this patch?

I compared the file xen/xen and on both arm32/arm64 the binary is smaller of about 2%.

I think this is because previously gcc wasn't merge the string when BUG_ON was used in inline function.

BTW this series won't work correctly because of XSA-102 on ARM64 when WARN_ON/BUG_ON/ASSERT is used in startup code.
This is because the "current" is set to a debug sanity value.

Quoted 5 lines
Quoted 2 lines

Do you plan to send the series to add support for 32-bit userspace on 64-bits kernels for Xen 4.5?

I must confess I'd forgotten about it. I'll dig it out and brush the cobwebs off ASAP.

Thanks, I saw the series in my inbox. I will give a look today or tomorrow.

Quoted 3 lines

In the meantime can the test at the head of do_trap_hypervisor be extended to DTRT with traps from EL2 (i.e. skip the rest of the expression)?

I plan to do smth like:

if ( guest_mode(regs) && ... )

Regards,

Signature · 1 line
Julien Grall
#6 lore raw
In reply to Julien Grall
Quoted 9 lines · Julien Grall

Hi Ian,

Quoted 3 lines · Ian Campbell
Quoted 1 line

+ /* PC is always 4-byte align, as Xen is using ARM instruction set */

"aligned"

Ok.

Quoted 8 lines
Quoted 3 lines

Is it worth a check here? I presume the nested fault if PC were misaligned would be pretty exciting, print+goto die would seem appropriate.

I don't think so. The "undefined instruction" is only used when the processor is unable to decode the instruction. If it fails to load the instruction (because a wrong memory address, pc misaligned), Xen would have received a "prefetch abort".

I was thinking of something either wrongly or rightly transitioning to thumb mode. It'd have to be something horrible like a broken firmware returning from a PSCI call in the wrong mode or something like that though.

Quoted 10 lines
Quoted 8 lines

xen/arch/arm/traps.c void do_unexpected_trap(const char *msg, struct cpu_user_regs *regs) · line 1082

Quoted 6 lines
  }> +#ifdef CONFIG_ARM_64
xen/arch/arm/traps.c +5 -0
+static void do_trap_brk(struct cpu_user_regs *regs, union hsr hsr)
+{
+    /* HCR_EL2.TGE and MDCR_EL2.TDE are not set so we never receive
+     * software breakpoint exception for EL1 and EL0 here
+     */

BUG_ON? ;-O

Sounds a good plan.

Just be careful that it won't recurse ;-)

Quoted 9 lines
Quoted 9 lines
Quoted 2 lines

+/* Many version of GCC doesn't support the asm %c parameter which would

"versions".

Quoted 3 lines

+ * be preferable to this unpleasantness. We use mergeable string
+ * sections to avoid multiple copies of the string appearing in the
+ * Xen image.

Quoted 7 lines
Quoted 1 line

OOI what is the size increase of the final (stripped) binary with this patch?

I compared the file xen/xen and on both arm32/arm64 the binary is smaller of about 2%.

I think this is because previously gcc wasn't merge the string when BUG_ON was used in inline function.

Not what I expected, but great!

Ian.

#7 lore raw

Hi Ian,

Quoted 19 lines · Ian Campbell
Quoted 9 lines · Julien Grall

Hi Ian,

Quoted 3 lines · Ian Campbell
Quoted 1 line

+ /* PC is always 4-byte align, as Xen is using ARM instruction set */

"aligned"

Ok.

Quoted 8 lines
Quoted 3 lines

Is it worth a check here? I presume the nested fault if PC were misaligned would be pretty exciting, print+goto die would seem appropriate.

I don't think so. The "undefined instruction" is only used when the processor is unable to decode the instruction. If it fails to load the instruction (because a wrong memory address, pc misaligned), Xen would have received a "prefetch abort".

Quoted 4 lines

I was thinking of something either wrongly or rightly transitioning to thumb mode. It'd have to be something horrible like a broken firmware returning from a PSCI call in the wrong mode or something like that though.

The thumb mode is stored in SPSR which is a banked registers. Your use case can only happen if the secure mode decides to modify the registers of the hypervisor. It would be very stupid from the firmware and other bad things could happen.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

xen/arch/arm/traps.c void do_unexpected_trap(const char *msg, struct cpu_user_regs *regs) · line 1082

Quoted 6 lines
   }> +#ifdef CONFIG_ARM_64
xen/arch/arm/traps.c +5 -0
+static void do_trap_brk(struct cpu_user_regs *regs, union hsr hsr)
+{
+    /* HCR_EL2.TGE and MDCR_EL2.TDE are not set so we never receive
+     * software breakpoint exception for EL1 and EL0 here
+     */

BUG_ON? ;-O

Quoted 3 lines
Quoted 1 line

Sounds a good plan.

Just be careful that it won't recurse ;-)

Panic should never go in exception mode, so I will use it instead.

Regards,

Signature · 1 line
Julien Grall
#8 lore raw
Quoted 27 lines · Julien Grall

Hi Ian,

Quoted 19 lines · Ian Campbell
Quoted 9 lines · Julien Grall

Hi Ian,

Quoted 3 lines · Ian Campbell
Quoted 1 line

+ /* PC is always 4-byte align, as Xen is using ARM instruction set */

"aligned"

Ok.

Quoted 8 lines
Quoted 3 lines

Is it worth a check here? I presume the nested fault if PC were misaligned would be pretty exciting, print+goto die would seem appropriate.

I don't think so. The "undefined instruction" is only used when the processor is unable to decode the instruction. If it fails to load the instruction (because a wrong memory address, pc misaligned), Xen would have received a "prefetch abort".

Quoted 4 lines

I was thinking of something either wrongly or rightly transitioning to thumb mode. It'd have to be something horrible like a broken firmware returning from a PSCI call in the wrong mode or something like that though.

Quoted 4 lines

The thumb mode is stored in SPSR which is a banked registers. Your use case can only happen if the secure mode decides to modify the registers of the hypervisor. It would be very stupid from the firmware and other bad things could happen.

Yes, as I said it would need to be horribly broken firmware. I wouldn't rule out the ability of firmware to do horribly broken things though.

Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

xen/arch/arm/traps.c void do_unexpected_trap(const char *msg, struct cpu_user_regs *regs) · line 1082

Quoted 6 lines
   }> +#ifdef CONFIG_ARM_64
xen/arch/arm/traps.c +5 -0
+static void do_trap_brk(struct cpu_user_regs *regs, union hsr hsr)
+{
+    /* HCR_EL2.TGE and MDCR_EL2.TDE are not set so we never receive
+     * software breakpoint exception for EL1 and EL0 here
+     */

BUG_ON? ;-O

Quoted 3 lines
Quoted 1 line

Sounds a good plan.

Just be careful that it won't recurse ;-)

Panic should never go in exception mode, so I will use it instead.

Regards,

#9 lore raw

Hi Ian,

Quoted 28 lines · Ian Campbell
Quoted 27 lines · Julien Grall

Hi Ian,

Quoted 23 lines · Ian Campbell
Quoted 9 lines · Julien Grall

Hi Ian,

Quoted 3 lines · Ian Campbell
Quoted 1 line

+ /* PC is always 4-byte align, as Xen is using ARM instruction set */

"aligned"

Ok.

Quoted 8 lines
Quoted 3 lines

Is it worth a check here? I presume the nested fault if PC were misaligned would be pretty exciting, print+goto die would seem appropriate.

I don't think so. The "undefined instruction" is only used when the processor is unable to decode the instruction. If it fails to load the instruction (because a wrong memory address, pc misaligned), Xen would have received a "prefetch abort".

I was thinking of something either wrongly or rightly transitioning to thumb mode. It'd have to be something horrible like a broken firmware returning from a PSCI call in the wrong mode or something like that though.

Quoted 7 lines
Quoted 4 lines

The thumb mode is stored in SPSR which is a banked registers. Your use case can only happen if the secure mode decides to modify the registers of the hypervisor. It would be very stupid from the firmware and other bad things could happen.

Yes, as I said it would need to be horribly broken firmware. I wouldn't rule out the ability of firmware to do horribly broken things though.

Ok. I will add a check and directly go to die.

Regards,

Signature · 1 line
Julien Grall