Skip to contentSkip to conversation

xen/arm64: correctly emulate the {w, x}zr registers

PATCH In review Ian Campbell 5 participants 7 messages Jan 15, 2016 – Mar 24, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen/arm64: correctly emulate the {w, x}zr registers

From: Julien Grall <[email protected]>

On AArch64, encoding 31 for an R<n> in the HSR is used to represent either {w,x}sp or {w,x}zr (See C1.2.4 in ARM DDI 0486A.d) depending on how the register field is interpreted by the instruction.

All the instructions trapped by Xen (either via a sysreg access or data abort) interpret encoding 31 as {w,x}zr. Therefore we don't have to worry about the possibility that a trap could refer to sp or about decoding the instruction.

For example AArch64 LDR and STR can have zr in the source/target register <Xt>, but never sp. sp can be present in the destination pointer( i.e. "[sp]"), but that would be represented by the value of FAR_EL2, not in the HSR.

For AArch32 it is possible for a LDR to target the PC, but this would not result in a valid ISS in the HSR register. However this could only occur if loading or storing the PC to MMIO, which we simply choose not to support for now.

Finally, features such as xenaccess can lead to us trapping on arbitrary instructions accessing RAM and not just for MMIO. However in many such cases HSR.ISS is not valid and in general features such as xenaccess do not rely on the nature of the specific instruction, they resolve the fault (via information found elsewhere e.g. FAR_EL2) without needing to know anything about the instruction which triggered the trap.

The register zr represents the zero register, i.e it will always return 0 and write to it is ignored. To properly handle this property, 2 new helpers have been introduced {get,set}_user_reg to read/write a value from/to a register. All the calls to select_user_reg have been replaced by these 2 helpers.

Furthermore, the code to emulate encoding 31 in select_user_reg has been dropped because it was invalid. For Aarch64 context, the encoding is used for sp or zr. For AArch32 context, the ISS won't be valid for data abort from AArch32 using r15 (i.e pc) as source/destination (See D7-1881 ARM DDI 0487A.d, note the validity is more restrictive than on ARMv7). It's also not possible to use r15 in co-processor instructions.

This patch fixes setting MMIO register and sysreg to a random value (actually PC) instead of zero by something like:

*((volatile int*)reg) = 0;

compilers tend to generate "str wzr, [xx]" here.

Added BUG_ON to select_user_reg for {w,x}zr case.

Clarified bits of the commit message by attempting to enumerate some of the cases which might be a concern in order to show why they are not.

---
v2: ijc: Took over the series, changes above.

---
 xen/arch/arm/io.c          |  34 +++++++++----
 xen/arch/arm/traps.c       | 122 +++++++++++++++++++++++++++++----------------
 xen/arch/arm/vgic-v3.c     |   3 +-
 xen/arch/arm/vtimer.c      |  59 +++++++++++++++++-----
 xen/include/asm-arm/regs.h |   7 +--
 5 files changed, 154 insertions(+), 71 deletions(-)
xen/arch/arm/io.c +25 -9
index 7e29943..0156755 100644
@@ -24,12 +24,19 @@
 #include <asm/mmio.h>
 
 static int handle_read(const struct mmio_handler *handler, struct vcpu *v,
-                       mmio_info_t *info, register_t *r)
+                       mmio_info_t *info)
 {
     const struct hsr_dabt dabt = info->dabt;
+    struct cpu_user_regs *regs = guest_cpu_user_regs();
+    /*
+     * Initialize to zero to avoid leaking data if there is an
+     * implementation error in the emulation (such as not correctly
+     * setting r).
+     */
+    register_t r = 0;
     uint8_t size = (1 << dabt.size) * 8;
 
-    if ( !handler->ops->read(v, info, r, handler->priv) )
+    if ( !handler->ops->read(v, info, &r, handler->priv) )
         return 0;
 
     /*
@@ -37,7 +44,7 @@ static int handle_read(const struct mmio_handler *handler, struct vcpu *v,
      * Note that we expect the read handler to have zeroed the bits
      * outside the requested access size.
      */
-    if ( dabt.sign && (*r & (1UL << (size - 1))) )
+    if ( dabt.sign && (r & (1UL << (size - 1))) )
     {
         /*
          * We are relying on register_t using the same as
@@ -45,21 +52,30 @@ static int handle_read(const struct mmio_handler *handler, struct vcpu *v,
          * code smaller.
          */
         BUILD_BUG_ON(sizeof(register_t) != sizeof(unsigned long));
-        *r |= (~0UL) << size;
+        r |= (~0UL) << size;
     }
 
+    set_user_reg(regs, dabt.reg, r);
+
     return 1;
 }
 
+static int handle_write(const struct mmio_handler *handler, struct vcpu *v,
+                        mmio_info_t *info)
+{
+    const struct hsr_dabt dabt = info->dabt;
+    struct cpu_user_regs *regs = guest_cpu_user_regs();
+
+    return handler->ops->write(v, info, get_user_reg(regs, dabt.reg),
+                               handler->priv);
+}
+
 int handle_mmio(mmio_info_t *info)
 {
     struct vcpu *v = current;
     int i;
     const struct mmio_handler *handler = NULL;
     const struct vmmio *vmmio = &v->domain->arch.vmmio;
-    struct hsr_dabt dabt = info->dabt;
-    struct cpu_user_regs *regs = guest_cpu_user_regs();
-    register_t *r = select_user_reg(regs, dabt.reg);
 
     for ( i = 0; i < vmmio->num_entries; i++ )
     {
@@ -74,9 +90,9 @@ int handle_mmio(mmio_info_t *info)
         return 0;
 
     if ( info->dabt.write )
-        return handler->ops->write(v, info, *r, handler->priv);
+        return handle_write(handler, v, info);
     else
-        return handle_read(handler, v, info, r);
+        return handle_read(handler, v, info);
 }
 
 void register_mmio_handler(struct domain *d,
xen/arch/arm/traps.c +80 -42

320 lines not on this page · raw message

xen/arch/arm/vgic-v3.c +1 -2
index dba2449..b37a7c0 100644
@@ -1304,7 +1304,6 @@ static int vgic_v3_emulate_sysreg(struct cpu_user_regs *regs, union hsr hsr)
 {
     struct vcpu *v = current;
     struct hsr_sysreg sysreg = hsr.sysreg;
-    register_t *r = select_user_reg(regs, sysreg.reg);
 
     ASSERT (hsr.ec == HSR_EC_SYSREG);
 
@@ -1318,7 +1317,7 @@ static int vgic_v3_emulate_sysreg(struct cpu_user_regs *regs, union hsr hsr)
     case HSR_SYSREG_ICC_SGI1R_EL1:
         /* WO */
         if ( !sysreg.read )
-            return vgic_v3_to_sgi(v, *r);
+            return vgic_v3_to_sgi(v, get_user_reg(regs, sysreg.reg));
         else
         {
             gprintk(XENLOG_WARNING, "Reading SGI1R_EL1 - WO register\n");
xen/arch/arm/vtimer.c +46 -13

109 lines not on this page · raw message

xen/include/asm-arm/regs.h +2 -5
index 56d53d6..2440edb 100644
@@ -50,11 +50,8 @@
 
 #define return_reg(v) ((v)->arch.cpu_info->guest_cpu_user_regs.r0)
 
-/*
- * Returns a pointer to the given register value in regs, taking the
- * processor mode (CPSR) into account.
- */
-extern register_t *select_user_reg(struct cpu_user_regs *regs, int reg);
+register_t get_user_reg(struct cpu_user_regs *regs, int reg);
+void set_user_reg(struct cpu_user_regs *regs, int reg, register_t val);
 
 #endif
 
Signature · 1 line
2.1.4
Reviewed-by on v2 1/1
#1 lore raw
Quoted 54 lines · Ian Campbell

From: Julien Grall <[email protected]>

On AArch64, encoding 31 for an R<n> in the HSR is used to represent either {w,x}sp or {w,x}zr (See C1.2.4 in ARM DDI 0486A.d) depending on how the register field is interpreted by the instruction.

All the instructions trapped by Xen (either via a sysreg access or data abort) interpret encoding 31 as {w,x}zr. Therefore we don't have to worry about the possibility that a trap could refer to sp or about decoding the instruction.

For example AArch64 LDR and STR can have zr in the source/target register <Xt>, but never sp. sp can be present in the destination pointer( i.e. "[sp]"), but that would be represented by the value of FAR_EL2, not in the HSR.

For AArch32 it is possible for a LDR to target the PC, but this would not result in a valid ISS in the HSR register. However this could only occur if loading or storing the PC to MMIO, which we simply choose not to support for now.

Finally, features such as xenaccess can lead to us trapping on arbitrary instructions accessing RAM and not just for MMIO. However in many such cases HSR.ISS is not valid and in general features such as xenaccess do not rely on the nature of the specific instruction, they resolve the fault (via information found elsewhere e.g. FAR_EL2) without needing to know anything about the instruction which triggered the trap.

The register zr represents the zero register, i.e it will always return 0 and write to it is ignored. To properly handle this property, 2 new helpers have been introduced {get,set}_user_reg to read/write a value from/to a register. All the calls to select_user_reg have been replaced by these 2 helpers.

Furthermore, the code to emulate encoding 31 in select_user_reg has been dropped because it was invalid. For Aarch64 context, the encoding is used for sp or zr. For AArch32 context, the ISS won't be valid for data abort from AArch32 using r15 (i.e pc) as source/destination (See D7-1881 ARM DDI 0487A.d, note the validity is more restrictive than on ARMv7). It's also not possible to use r15 in co-processor instructions.

This patch fixes setting MMIO register and sysreg to a random value (actually PC) instead of zero by something like:

*((volatile int*)reg) = 0;

compilers tend to generate "str wzr, [xx]" here.

Added BUG_ON to select_user_reg for {w,x}zr case.

Quoted 7 lines

Clarified bits of the commit message by attempting to enumerate some of the cases which might be a concern in order to show why they are not.

---
v2: ijc: Took over the series, changes above.

I read the patch and looks good to me. You can add my

I have a couple of minor comments, which you can ignore or address as you commit the patch.

Quoted 14 lines
xen/arch/arm/traps.c +14 -0
+register_t get_user_reg(struct cpu_user_regs *regs, int reg)
+{
+#ifdef CONFIG_ARM_64
+    /*
+     * For store/load and sysreg instruction, the encoding 31 always
+     * correspond to {w,x}zr which is the zero register.
+     */
+    if ( reg == 31 )
+        return 0;
+#endif
+
+    return *select_user_reg(regs, reg);
+}
+

xen/arch/arm/traps.c register_t *select_user_reg(struct cpu_user_regs *regs, int reg) · line 249

Quoted 10 lines
xen/arch/arm/traps.c +10 -0
+void set_user_reg(struct cpu_user_regs *regs, int reg, register_t value)
+{
+#ifdef CONFIG_ARM_64
+    /*
+     * For store/load and sysreg instruction, the encoding 31 always
+     * correspond to {w,x}zr which is the zero register.
+     */
+    if ( reg == 31 )
+        return;
+#endif

Couldn't we call a static inline function which is implemented differently on CONFIG_ARM_64 and CONFIG_ARM_32 to do the reg == 31 check?

xen/arch/arm/traps.c static void do_cp15_32(struct cpu_user_regs *regs, · line 1839

Quoted 13 lines
xen/arch/arm/traps.c +4 -0
+    *select_user_reg(regs, reg) = value;
+}
+
 static const char *decode_fsc(uint32_t fsc, int *level)
 {
     const char *msg = NULL;
@@ -1803,6 +1834,7 @@ static void do_cp15_32(struct cpu_user_regs *regs,
         inject_undef_exception(regs, hsr);
         return;
     }
+
     advance_pc(regs, hsr);
 }

Spurious change

#2 lore raw

Hi Stefano,

Quoted 8 lines · Stefano Stabellini

On Fri, 15 Jan 2016, Ian Campbell wrote:

I read the patch and looks good to me. You can add my

I have a couple of minor comments, which you can ignore or address as you commit the patch.

This patch fell through the crack. Some compilers may generate MMIO access with *zr, so we need this patch in Xen 4.7 (and potentially backport it).

Are you fine if the patch is not respined?

Regards,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#3 lore raw
Quoted 9 lines · Julien Grall

Hi Stefano,

Quoted 6 lines · Stefano Stabellini

On Fri, 15 Jan 2016, Ian Campbell wrote:

I read the patch and looks good to me. You can add my

Quoted 7 lines
Quoted 2 lines

I have a couple of minor comments, which you can ignore or address as you commit the patch.

This patch fell through the crack. Some compilers may generate MMIO access with *zr, so we need this patch in Xen 4.7 (and potentially backport it).

Are you fine if the patch is not respined?

Yes, I am fine with it as is.

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

#4 lore raw

(CC committers)

Quoted 13 lines · Stefano Stabellini
Quoted 12 lines · Julien Grall

Hi Stefano,

Quoted 8 lines · Stefano Stabellini

On Fri, 15 Jan 2016, Ian Campbell wrote:

I read the patch and looks good to me. You can add my

I have a couple of minor comments, which you can ignore or address as you commit the patch.

Quoted 6 lines
Quoted 4 lines

This patch fell through the crack. Some compilers may generate MMIO access with *zr, so we need this patch in Xen 4.7 (and potentially backport it).

Are you fine if the patch is not respined?

Yes, I am fine with it as is.

Thanks. Can someone commit this patch?

Regards,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#5 lore raw
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 24.03.16 at 12:12, <[email protected]> wrote:

(CC committers)

Quoted 13 lines · Stefano Stabellini
Quoted 12 lines · Julien Grall

Hi Stefano,

Quoted 8 lines · Stefano Stabellini

On Fri, 15 Jan 2016, Ian Campbell wrote:

I read the patch and looks good to me. You can add my

I have a couple of minor comments, which you can ignore or address as you commit the patch.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

This patch fell through the crack. Some compilers may generate MMIO access with *zr, so we need this patch in Xen 4.7 (and potentially backport it).

Are you fine if the patch is not respined?

Yes, I am fine with it as is.

Thanks. Can someone commit this patch?

I think after over two months this should simply be resubmitted (with the R-b, and possibly with the minor comments addressed). I for one don't have this in my mailbox anymore, and picking it up from the xen-devel archive would yield obfuscated email addresses.

Jan

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

#6 lore raw

Hi Jan,

Quoted 24 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 24.03.16 at 12:12, <[email protected]> wrote:

(CC committers)

Quoted 19 lines · Stefano Stabellini
Quoted 9 lines · Julien Grall

Hi Stefano,

Quoted 6 lines · Stefano Stabellini

On Fri, 15 Jan 2016, Ian Campbell wrote:

I read the patch and looks good to me. You can add my

Quoted 7 lines
Quoted 2 lines

I have a couple of minor comments, which you can ignore or address as you commit the patch.

This patch fell through the crack. Some compilers may generate MMIO access with *zr, so we need this patch in Xen 4.7 (and potentially backport it).

Are you fine if the patch is not respined?

Yes, I am fine with it as is.

Quoted 7 lines
Quoted 1 line

Thanks. Can someone commit this patch?

I think after over two months this should simply be resubmitted (with the R-b, and possibly with the minor comments addressed). I for one don't have this in my mailbox anymore, and picking it up from the xen-devel archive would yield obfuscated email addresses.

I will do it.

Cheers,

Signature · 6 lines
Julien Grall

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