From f9879f4ed198ca03a03bf69152f0158be6580252 Mon Sep 17 00:00:00 2001
From: Julien Grall <julien.grall@arm.com>
Date: Tue, 1 Oct 2019 13:07:53 +0100
Subject: [PATCH 2/5] xen/arm32: entry: Consolidate DEFINE_TRAP_ENTRY*() macros

The only difference between the two macros DEFINE_TRAP_ENTRY() and
DEFINE_TRAP_ENTRY_NOIRQ() is the list of interrupts to be unmasked.

While the macros are fairly small today, it will be necessary to add the
same code twice in follow-up patches.

To prevent too much duplication, a new assembly macros is introduced to
generate the body of a trap.

This is part of XSA-303.

Reported-by: Julien Grall <Julien.Grall@arm.com>
Signed-off-by: Julien Grall <julien.grall@arm.com>
Acked-by: Stefano Stabellini <sstabellini@kernel.org>
---
 xen/arch/arm/arm32/entry.S | 41 ++++++++++++++++++++++++-----------------
 1 file changed, 24 insertions(+), 17 deletions(-)

diff --git a/xen/arch/arm/arm32/entry.S b/xen/arch/arm/arm32/entry.S
index 3e320dc8ac..f6ba5a2082 100644
--- a/xen/arch/arm/arm32/entry.S
+++ b/xen/arch/arm/arm32/entry.S
@@ -116,30 +116,37 @@ abort_guest_exit_end:
 
         mov pc, lr
 
+
+        /*
+         * Macro to define a trap entry. The iflags is optional and
+         * corresponds to alist of interrupts (Asynchronous Abort, IRQ, FIQ)
+         * to unmask.
+         */
+        .macro vector trap, iflags=n
+        SAVE_ALL
+        .if \iflags != n
+        cpsie   \iflags
+        .endif
+        adr     lr, return_from_trap
+        mov     r0, sp
+        /*
+         * Save the stack pointer in r11. It will be restored after the
+         * trap has been handled (see return_from_trap).
+         */
+        mov     r11, sp
+        bic     sp, #7      /* Align the stack pointer (noop on guest trap) */
+        b       do_trap_\trap
+        .endm
+
 #define DEFINE_TRAP_ENTRY(trap)                                         \
         ALIGN;                                                          \
 trap_##trap:                                                            \
-        SAVE_ALL;                                                       \
-        cpsie i;        /* local_irq_enable */                          \
-        adr lr, return_from_trap;                                       \
-        mov r0, sp;                                                     \
-        /*                                                              \
-         * Save the stack pointer in r11. It will be restored after the \
-         * trap has been handled (see return_from_trap).                \
-         */                                                             \
-        mov r11, sp;                                                    \
-        bic sp, #7; /* Align the stack pointer (noop on guest trap) */  \
-        b do_trap_##trap
+        vector trap, iflags=i                                           \
 
 #define DEFINE_TRAP_ENTRY_NOIRQ(trap)                                   \
         ALIGN;                                                          \
 trap_##trap:                                                            \
-        SAVE_ALL;                                                       \
-        adr lr, return_from_trap;                                       \
-        mov r0, sp;                                                     \
-        mov r11, sp;                                                    \
-        bic sp, #7; /* Align the stack pointer (noop on guest trap) */  \
-        b do_trap_##trap
+        vector trap
 
         .align 5
 GLOBAL(hyp_traps_vector)
-- 
2.11.0

