Skip to contentSkip to conversation

xen/arm64: Emulate correctly the register {w, x}zr

PATCH In review Julien Grall 2 participants 15 messages Dec 11, 2015 – Jan 5, 2016

lore
Share & tools
lore
#0 lore raw

cover xen/arm64: Emulate correctly the register {w, x}zr

Hi all,

This patch series aims to fix the emulation of register {w,x}zr for ARM64.

A branch is available with all the patches applied:

git://xenbits.xen.org/people/julieng/xen-unstable.git branch handle-xzr-v1

Regards,

Julien Grall (4):
xen/arm: io: handle_read: Use a local variable to store dabt xen/arm64: Document the register mapping aarch64 <-> aarch32 xen/arm: vtimer: Introduce vtimer_emulate_sysreg{32,64} xen/arm64: Emulate correctly the register {w,x}zr

 xen/arch/arm/io.c                     |  37 ++++++++---
 xen/arch/arm/traps.c                  | 121 ++++++++++++++++++++++------------
 xen/arch/arm/vgic-v3.c                |   3 +-
 xen/arch/arm/vtimer.c                 |  96 ++++++++++++++++++++-------
 xen/include/asm-arm/arm64/processor.h |   7 +-
 xen/include/asm-arm/regs.h            |   7 +-
 6 files changed, 189 insertions(+), 82 deletions(-)
Signature · 1 line
2.1.4
#1 lore raw

PATCH 1/4 xen/arm: io: handle_read: Use a local variable to store dabt

Rather than getting dabt every time through info->dabt, introduce a local variable and use it.

Also fix a coding style error in the if condition.

---
 xen/arch/arm/io.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
xen/arch/arm/io.c +3 -2
index de5765a..7e29943 100644
@@ -26,7 +26,8 @@
 static int handle_read(const struct mmio_handler *handler, struct vcpu *v,
                        mmio_info_t *info, register_t *r)
 {
-    uint8_t size = (1 << info->dabt.size) * 8;
+    const struct hsr_dabt dabt = info->dabt;
+    uint8_t size = (1 << dabt.size) * 8;
 
     if ( !handler->ops->read(v, info, r, handler->priv) )
         return 0;
@@ -36,7 +37,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 ( info->dabt.sign && (*r & (1UL << (size - 1)) ))
+    if ( dabt.sign && (*r & (1UL << (size - 1))) )
     {
         /*
          * We are relying on register_t using the same as
Signature · 1 line
2.1.4
Acked-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] xen/arm: io: handle_read: Use a local variable to store dabt

Quoted 6 lines · Julien Grall

Rather than getting dabt every time through info->dabt, introduce a local variable and use it.

Also fix a coding style error in the if condition.

Quoted 28 lines

---
 xen/arch/arm/io.c | 5 +++--

 1 file changed, 3 insertions(+), 2 deletions(-)
xen/arch/arm/io.c +0 -0
index de5765a..7e29943 100644
@@ -26,7 +26,8 @@

 static int handle_read(const struct mmio_handler *handler, struct vcpu *v,
                        mmio_info_t *info, register_t *r)
 {
-    uint8_t size = (1 << info->dabt.size) * 8;
+    const struct hsr_dabt dabt = info->dabt;
+    uint8_t size = (1 << dabt.size) * 8;

     if ( !handler->ops->read(v, info, r, handler->priv) ) return 0;

xen/arch/arm/io.c +1 -1
@@ -36,7 +37,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 ( info->dabt.sign && (*r & (1UL << (size - 1)) ))
+    if ( dabt.sign && (*r & (1UL << (size - 1))) )
      {
          /*
Quoted 1 line

          * We are relying on register_t using the same as

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

#3 lore raw

PATCH 2/4 xen/arm64: Document the register mapping aarch64 <-> aarch32

In reply to Julien Grall

The mapping between aarch64 and aarch32 has not been chosen in random. It's based on D1.20.1 in ARM DDI 0487A.d.

The section is not obvious to find in the spec, so make it clear for the anyone else.

---
 xen/include/asm-arm/arm64/processor.h | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
xen/include/asm-arm/arm64/processor.h +6 -1
index 3a9c0cd..fef35a5 100644
@@ -15,7 +15,12 @@
 /* On stack VCPU state */
 struct cpu_user_regs
 {
-    /*         Aarch64       Aarch32 */
+    /*
+     * The mapping AArch64 <-> AArch32 is based on D1.20.1 in ARM DDI
+     * 0487A.d.
+     *
+     *         AArch64       AArch32
+     */
     __DECL_REG(x0,           r0/*_usr*/);
     __DECL_REG(x1,           r1/*_usr*/);
     __DECL_REG(x2,           r2/*_usr*/);
Signature · 1 line
2.1.4
Acked-by on v1 2/4
#4 lore raw

Re: [PATCH 2/4] xen/arm64: Document the register mapping aarch64 <-> aarch32

Quoted 7 lines · Julien Grall

The mapping between aarch64 and aarch32 has not been chosen in random. It's based on D1.20.1 in ARM DDI 0487A.d.

The section is not obvious to find in the spec, so make it clear for the anyone else.

Quoted 15 lines

---
 xen/include/asm-arm/arm64/processor.h | 7 ++++++-

 1 file changed, 6 insertions(+), 1 deletion(-)
xen/include/asm- renamed from xen/include/asm-arm/arm64/processor.h +0 -0

arm/arm64/processor.h
index 3a9c0cd..fef35a5 100644

xen/include/asm-arm/arm64/processor.h +0 -0
@@ -15,7 +15,12 @@
  /* On stack VCPU state */

 struct cpu_user_regs
 {
-    /*         Aarch64       Aarch32 */
+    /*

Quoted 8 lines

+     * The mapping AArch64 <-> AArch32 is based on D1.20.1 in ARM DDI +     * 0487A.d.
+     *
+     *         AArch64       AArch32
+     */
     __DECL_REG(x0,           r0/*_usr*/);
     __DECL_REG(x1,           r1/*_usr*/);
     __DECL_REG(x2,           r2/*_usr*/);

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

#5 lore raw

PATCH 3/4 xen/arm: vtimer: Introduce vtimer_emulate_sysreg{32, 64}

In reply to Julien Grall

Factorize the code to emulate a a 32-bit/64-bit sysreg in specific helpers.

While this is currently not necessary, it will be helpful in a following patch to handle properly some registers.

---
 xen/arch/arm/vtimer.c | 47 +++++++++++++++++++++++++++++++++--------------
 1 file changed, 33 insertions(+), 14 deletions(-)
xen/arch/arm/vtimer.c +33 -14
index 629feb4..b9c0b41 100644
@@ -302,11 +302,39 @@ static int vtimer_emulate_cp64(struct cpu_user_regs *regs, union hsr hsr)
 }
 
 #ifdef CONFIG_ARM_64
-static int vtimer_emulate_sysreg(struct cpu_user_regs *regs, union hsr hsr)
+typedef int (*vtimer_sysreg32_fn_t)(struct cpu_user_regs *regs, uint32_t *r,
+                                    int read);
+typedef int (*vtimer_sysreg64_fn_t)(struct cpu_user_regs *regs, uint64_t *r,
+                                    int read);
+
+static int vtimer_emulate_sysreg32(struct cpu_user_regs *regs, union hsr hsr,
+                                   vtimer_sysreg32_fn_t fn)
 {
     struct hsr_sysreg sysreg = hsr.sysreg;
     register_t *x = select_user_reg(regs, sysreg.reg);
-    uint32_t r = (uint32_t)*x;
+    uint32_t r = *x;
+    int ret;
+
+    ret = fn(regs, &r, sysreg.read);
+
+    if ( ret && sysreg.read )
+        *x = r;
+
+    return ret;
+}
+
+static int vtimer_emulate_sysreg64(struct cpu_user_regs *regs, union hsr hsr,
+                                   vtimer_sysreg64_fn_t fn)
+{
+    struct hsr_sysreg sysreg = hsr.sysreg;
+    uint64_t *x = select_user_reg(regs, sysreg.reg);
+
+    return fn(regs, x, sysreg.read);
+}
+
+static int vtimer_emulate_sysreg(struct cpu_user_regs *regs, union hsr hsr)
+{
+    struct hsr_sysreg sysreg = hsr.sysreg;
 
     if ( sysreg.read )
         perfc_incr(vtimer_sysreg_reads);
@@ -316,20 +344,11 @@ static int vtimer_emulate_sysreg(struct cpu_user_regs *regs, union hsr hsr)
     switch ( hsr.bits & HSR_SYSREG_REGS_MASK )
     {
     case HSR_SYSREG_CNTP_CTL_EL0:
-        if ( !vtimer_cntp_ctl(regs, &r, sysreg.read) )
-            return 0;
-        if ( sysreg.read )
-            *x = r;
-        return 1;
+        return vtimer_emulate_sysreg32(regs, hsr, vtimer_cntp_ctl);
     case HSR_SYSREG_CNTP_TVAL_EL0:
-        if ( !vtimer_cntp_tval(regs, &r, sysreg.read) )
-            return 0;
-        if ( sysreg.read )
-            *x = r;
-        return 1;
-
+        return vtimer_emulate_sysreg32(regs, hsr, vtimer_cntp_tval);
     case HSR_SYSREG_CNTP_CVAL_EL0:
-        return vtimer_cntp_cval(regs, x, sysreg.read);
+        return vtimer_emulate_sysreg64(regs, hsr, vtimer_cntp_cval);
 
     default:
         return 0;
Signature · 1 line
2.1.4
Acked-by on v1 3/4
#6 lore raw

Re: [PATCH 3/4] xen/arm: vtimer: Introduce vtimer_emulate_sysreg{32, 64}

Quoted 5 lines · Julien Grall

Factorize the code to emulate a a 32-bit/64-bit sysreg in specific helpers.

While this is currently not necessary, it will be helpful in a following patch to handle properly some registers.

Looks like it would be potentially be usefully more generic too, but not now.

Quoted 1 line
#7 lore raw

Re: [PATCH 3/4] xen/arm: vtimer: Introduce vtimer_emulate_sysreg{32, 64}

Hi Ian,

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

Factorize the code to emulate a a 32-bit/64-bit sysreg in specific helpers.

While this is currently not necessary, it will be helpful in a following patch to handle properly some registers.

Looks like it would be potentially be usefully more generic too, but not now.

I though about it and wanted to rework the sysreg emulation at the same time. Although it was too much for this series.

Quoted 3 lines
Quoted 1 line

Thanks!

Regards,

Signature · 1 line
Julien Grall
#8 lore raw

PATCH 4/4 xen/arm64: Emulate correctly the register {w, x}zr

In reply to Julien Grall

On AArch64, the encoding 31 could be used to represent {x,w}sp or {x,w}zr (See C1.2.4 in ARM DDI 0486A.d). The choice will depend how the register field is interpreted by the instruction.

All the instructions trapped by Xen (either via a sysreg access or data abort) interprets the encoding 31 as zr. Therefore we don't have to worry about decoding the instruction.

The register zr reprents the zero register, i.e it will always return 0 and write to it is ignored. To handle properly this properties, 2 new helpers have been introduced {get,set}_user_reg to read/write a value from/to a register. All the call 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.

---
 xen/arch/arm/io.c          |  34 +++++++++----
 xen/arch/arm/traps.c       | 121 +++++++++++++++++++++++++++++----------------
 xen/arch/arm/vgic-v3.c     |   3 +-
 xen/arch/arm/vtimer.c      |  59 +++++++++++++++++-----
 xen/include/asm-arm/regs.h |   7 +--
 5 files changed, 153 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 +79 -42

319 lines not on this page · raw message

xen/arch/arm/vgic-v3.c +1 -2
index 985e866..56f98ef 100644
@@ -1305,7 +1305,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);
 
@@ -1319,7 +1318,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
#9 lore raw
Quoted 7 lines · Julien Grall

On AArch64, the encoding 31 could be used to represent {x,w}sp or {x,w}zr (See C1.2.4 in ARM DDI 0486A.d). The choice will depend how the register field is interpreted by the instruction.

All the instructions trapped by Xen (either via a sysreg access or data abort) interprets the encoding 31 as zr. Therefore we don't have to worry about decoding the instruction.

Is decoding the only way we could tell? i.e. there's no indication in the syndrome reg?

Is there no way a data abort could result from an access which involved SP rather than XZ? e.g. if the OS was to (stupidly) point SP at an MMIO area and then try a stp x0, x1, [sp] for example? Ah, perhaps in that case we would get a trap with x0 or x1 but sp would be in the FAR and not in the HSR?

Or maybe a less lunatic case, could xenaccess not result in a faulting stack access? I suppose the answer there is that xenaccess faults are handled without actually caring which register it was and then the instruction is replayed in guest context?

Aside: if an stp traps, does the h/v get two exits, one for each register? I suppose it must.

Quoted 1 line

The register zr reprents the zero register, i.e it will always

"represents"

Quoted 1 line

return 0 and write to it is ignored. To handle properly this properties,

"these properties"

Quoted 2 lines

2 new helpers have been introduced {get,set}_user_reg to read/write a value from/to a register. All the call to select_user_reg have been

calls

Quoted 8 lines

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.

Does this "Just Work" for arm32 Xen then?

What happens if an aarch32 guest accesses r15 in one of these ways on an aarch64 hypervisor? Is it verboten in ARM v8 or something else? E1.2.3 describes it as deprecated, but not forbidden, but I suspect that "and the source address of the ldr is in MMIO space" is not covered there or something.

Quoted 5 lines
xen/arch/arm/traps.c +0 -0
@@ -207,16 +214,42 @@ register_t *select_user_reg(struct cpu_user_regs
 *regs, int reg)
      }
  #undef REGOFFS
  #else

xen/arch/arm/traps.c register_t *select_user_reg(struct cpu_user_regs · line 222

Quoted 10 lines
xen/arch/arm/traps.c +3 -5
-    /* In 64 bit the syndrome register contains the AArch64 register
-     * number even if the trap was from AArch32 mode. Except that
-     * AArch32 R15 (PC) is encoded as 0b11111.
+    /*
+     * On 64-bit the syndrome register contains the register index as
+     * viewed in AArch64 state even if the trap was from AArch32 mode.
       */
-    if ( reg == 0x1f /* && is aarch32 guest */)
-        return &regs->pc;
      return &regs->x0 + reg;

If reg were == 0x1f here then it would end up accessing regs->sp, which is only valid for traps from Xen to Xen (i.e. interrupt stack frames), whichif it were to somehow happen would lead to some rather interesting behaviour I fear. I'm pretty sure it can't happen, but I'd be happier with an explicit assert/BUG_ON.

Alternatively we could return NULL here to represent XZR and handle that appropriately in the {get,set}_user_reg, replacing the check for reg == 31 in both places.

(FWIW the actual guest stack pointer state is in either SP_EL{0,1} or the aarch sp_* in x13, x17 etc.)

Quoted 22 lines
(quoted diff) +0 -0
  #endif
  }

xen/arch/arm/traps.c +19 -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);
+}
+
+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

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

Quoted 10 lines
xen/arch/arm/traps.c +9 -0
+     * correspond to {w,x}zr which is the zero register.
+     */
+    if ( reg == 31 )
+        return;
+#endif
+
+    *select_user_reg(regs, reg) = value;
+}
+
[...]

All of the other mostly mechanical changes look OK from a quick glance.

Ian.

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

#10 lore raw

Hi Ian,

Quoted 11 lines · Ian Campbell
Quoted 7 lines · Julien Grall

On AArch64, the encoding 31 could be used to represent {x,w}sp or {x,w}zr (See C1.2.4 in ARM DDI 0486A.d). The choice will depend how the register field is interpreted by the instruction.

All the instructions trapped by Xen (either via a sysreg access or data abort) interprets the encoding 31 as zr. Therefore we don't have to worry about decoding the instruction.

Is decoding the only way we could tell? i.e. there's no indication in the syndrome reg?

Unfortunately yes, the syndrome reg will only contain the encoding of the transferred register.

However, by looking to the list of instruction that could be trapped by Xen and contain valid ISS, the encoding 31 will always mean zr.

Quoted 5 lines

Is there no way a data abort could result from an access which involved SP rather than XZ? e.g. if the OS was to (stupidly) point SP at an MMIO area and then try a stp x0, x1, [sp] for example? Ah, perhaps in that case we would get a trap with x0 or x1 but sp would be in the FAR and not in the HSR?

First if the instruction stp trap into Xen, the ISS would be invalid.

For AArch64, only loads and store of a single general-purpose register would result to a valid ISS (see D7-1880 in ARM DDI 0487A.d).

If we take an instruction producing a valid ISS:

ldr x0, [x2]
str x0, [x2]

The ISS will always contains the transfered register, i.e x0 here. The faulting address (x2) will be in FAR.

Quoted 5 lines

Or maybe a less lunatic case, could xenaccess not result in a faulting stack access? I suppose the answer there is that xenaccess faults are handled without actually caring which register it was and then the instruction is replayed in guest context?

xenaccess can't rely on the instruction syndrome because the fault may happen with instruction producing invalid ISS.

If we ever need the register in xenaccess we have to decode by hand every instruction.

Quoted 26 lines

Aside: if an stp traps, does the h/v get two exits, one for each register? I suppose it must.

Quoted 1 line

The register zr reprents the zero register, i.e it will always

"represents"

Quoted 1 line

return 0 and write to it is ignored. To handle properly this properties,

"these properties"

Quoted 2 lines

2 new helpers have been introduced {get,set}_user_reg to read/write a value from/to a register. All the call to select_user_reg have been

calls

Quoted 8 lines

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.

Quoted 7 lines

Does this "Just Work" for arm32 Xen then?

What happens if an aarch32 guest accesses r15 in one of these ways on an aarch64 hypervisor? Is it verboten in ARM v8 or something else? E1.2.3 describes it as deprecated, but not forbidden, but I suspect that "and the source address of the ldr is in MMIO space" is not covered there or something.

Based on the description of the ISS encoding for data abort, the syndrome would be invalid.

Our data abort handler will inject a data abort to the guest for any invalid instruction.

If we ever want to support the guest to write pc in an emulated MMIO, we would have to decode the instruction.

Quoted 14 lines
Quoted 14 lines
xen/arch/arm/traps.c +3 -4
@@ -207,16 +214,42 @@ register_t *select_user_reg(struct cpu_user_regs
 *regs, int reg)
     }
 #undef REGOFFS
 #else
-    /* In 64 bit the syndrome register contains the AArch64 register
-     * number even if the trap was from AArch32 mode. Except that
-     * AArch32 R15 (PC) is encoded as 0b11111.
+    /*
+     * On 64-bit the syndrome register contains the register index as
+     * viewed in AArch64 state even if the trap was from AArch32 mode.
      */
-    if ( reg == 0x1f /* && is aarch32 guest */)
Quoted 8 lines

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

Quoted 2 lines
xen/arch/arm/traps.c +0 -1
-        return &regs->pc;
     return &regs->x0 + reg;

If reg were == 0x1f here then it would end up accessing regs->sp, which is only valid for traps from Xen to Xen (i.e. interrupt stack frames), whichif it were to somehow happen would lead to some rather interesting behaviour I fear. I'm pretty sure it can't happen, but I'd be happier with an explicit assert/BUG_ON.

I will had a BUG_ON Here.

Quoted 3 lines

Alternatively we could return NULL here to represent XZR and handle that appropriately in the {get,set}_user_reg, replacing the check for reg == 31 in both places.

I would rather avoid select_user_regs to return NULL. The explicit encoding check in {get,set}_user_reg is better.

Regards,

Signature · 1 line
Julien Grall
#11 lore raw
Quoted 1 line · Julien Grall

Hi Ian,

Thanks for the replies, I think mostly adding the further exposition from this thread to the commit message would make this patch OK.

Quoted 17 lines · Ian Campbell
Quoted 6 lines · Julien Grall
Quoted 5 lines

On AArch64, the encoding 31 could be used to represent {x,w}sp or {x,w}zr (See C1.2.4 in ARM DDI 0486A.d). The choice will depend how the
register field is interpreted by the instruction.

Quoted 8 lines
Quoted 4 lines

All the instructions trapped by Xen (either via a sysreg access or data
abort) interprets the encoding 31 as zr. Therefore we don't have to worry about decoding the instruction.

Is decoding the only way we could tell? i.e. there's no indication in the
syndrome reg?

Quoted 5 lines

Unfortunately yes, the syndrome reg will only contain the encoding of the transferred register.

However, by looking to the list of instruction that could be trapped by Xen and contain valid ISS, the encoding 31 will always mean zr.

Great.

Quoted 15 lines
Quoted 9 lines

Is there no way a data abort could result from an access which involved SP
rather than XZ? e.g. if the OS was to (stupidly) point SP at an MMIO area
and then try a stp x0, x1, [sp] for example? Ah, perhaps in that case we
would get a trap with x0 or x1 but sp would be in the FAR and not in the
HSR?

First if the instruction stp trap into Xen, the ISS would be invalid.

For AArch64, only loads and store of a single general-purpose register would result to a valid ISS (see D7-1880 in ARM DDI 0487A.d).

Quoted 7 lines

If we take an instruction producing a valid ISS:

ldr x0, [x2]
str x0, [x2]

The ISS will always contains the transfered register, i.e x0 here. The faulting address (x2) will be in FAR.

Great. And the SP can only appear as the [sp] slot, so ldr x0 [sp] but never ldr sp [x0], so that's ok for us too.

Quoted 8 lines

Quoted 4 lines

Or maybe a less lunatic case, could xenaccess not result in a faulting stack access? I suppose the answer there is that xenaccess faults are handled without actually caring which register it was and then the instruction is replayed in guest context?

xenaccess can't rely on the instruction syndrome because the fault may happen with instruction producing invalid ISS.

Right, this was my theory too.

Quoted 6 lines
Quoted 5 lines

What happens if an aarch32 guest accesses r15 in one of these ways on an aarch64 hypervisor? Is it verboten in ARM v8 or something else? E1.2.3 describes it as deprecated, but not forbidden, but I suspect that "and the source address of the ldr is in MMIO space" is not covered there or something.

Quoted 8 lines

Based on the description of the ISS encoding for data abort, the syndrome would be invalid.

Our data abort handler will inject a data abort to the guest for any invalid instruction.

If we ever want to support the guest to write pc in an emulated MMIO, we would have to decode the instruction.

I doubt we would want to, but so long as the worst which can happen is that we kill the guest (rather than the host) then I'm happy.

Quoted 5 lines
Quoted 2 lines

Alternatively we could return NULL here to represent XZR and handle that appropriately in the {get,set}_user_reg, replacing the check for reg == 31 in both places.

I would rather avoid select_user_regs to return NULL. The explicit encoding check in {get,set}_user_reg is better.

OK.

Ian.

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

#12 lore raw
In reply to Julien Grall

Hi Ian,

Quoted 6 lines · Julien Grall

Hi all,

This patch series aims to fix the emulation of register {w,x}zr for ARM64.

A branch is available with all the patches applied:

Quoted 8 lines

git://xenbits.xen.org/people/julieng/xen-unstable.git branch handle-xzr-v1

Regards,

Julien Grall (4):
xen/arm: io: handle_read: Use a local variable to store dabt xen/arm64: Document the register mapping aarch64 <-> aarch32 xen/arm: vtimer: Introduce vtimer_emulate_sysreg{32,64}

The first 3 patches are acked. Would it be possible to commit them so I only need to resent patch #4?

Regards,

Signature · 1 line
Julien Grall
#13 lore raw
Quoted 15 lines · Julien Grall

Hi Ian,

Quoted 12 lines · Julien Grall

Hi all,

This patch series aims to fix the emulation of register {w,x}zr for ARM64.

A branch is available with all the patches applied:

git://xenbits.xen.org/people/julieng/xen-unstable.git branch handle- xzr-v1

Regards,

Quoted 7 lines
Quoted 4 lines

Julien Grall (4):
  xen/arm: io: handle_read: Use a local variable to store dabt xen/arm64: Document the register mapping aarch64 <-> aarch32 xen/arm: vtimer: Introduce vtimer_emulate_sysreg{32,64}

The first 3 patches are acked. Would it be possible to commit them so I only need to resent patch #4?

I've not got time to commit anything today, but please just resend #4 (as a reply to v1) and I'll try and DTRT upon commit.

Ian.

Quoted 3 lines

Regards,

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

#14 lore raw
Quoted 21 lines · Ian Campbell
Quoted 20 lines · Julien Grall

Hi Ian,

Quoted 16 lines · Julien Grall

Hi all,

This patch series aims to fix the emulation of register {w,x}zr for ARM64.

A branch is available with all the patches applied:

git://xenbits.xen.org/people/julieng/xen-unstable.git branch handle- xzr-v1

Regards,

Julien Grall (4):
  xen/arm: io: handle_read: Use a local variable to store dabt xen/arm64: Document the register mapping aarch64 <-> aarch32 xen/arm: vtimer: Introduce vtimer_emulate_sysreg{32,64}

Quoted 5 lines
Quoted 2 lines

The first 3 patches are acked. Would it be possible to commit them so I only need to resent patch #4?

I've not got time to commit anything today, but please just resend #4 (as a reply to v1) and I'll try and DTRT upon commit.

I've now applied the first three. However since you have since moved on I won't expect a resend of #4 (or even that you will see this mail other than in the archives) so I'll add this to my own TODO list.

Ian.

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