Skip to contentSkip to conversation

xen/arm: Add emulation of Debug Data Transfer Registers

PATCH Superseded Ayan Kumar Halder 5 participants 13 messages Feb 20, 2024 – Feb 26, 2024

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover xen/arm: Add emulation of Debug Data Transfer Registers

Hi,

Refer https://lore.kernel.org/all/alpine.DEB.2.22.394.2312071341540.1265976@ubuntu-linux-20-04-desktop/T/
for the previous discussion on this issue.

Also, the linux earlycon hvc driver has been fixed.
See https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty.git/commit/?h=tty-next&id=0ec058ece2f933aed66b76bd5cb9b5e6764853c3

Changes from v1:-
1. Split the change across 3 patches as per the design consensus.

v2 :-
1. Reordered the patches.

v3 :-
1. Change mentioned in individual patches.

v4 :-
1. Change mentioned in individual patches.

Ayan Kumar Halder (2):
xen/arm: Introduce CONFIG_PARTIAL_EMULATION and "partial-emulation"
cmd option
xen/arm: arm32: Add emulation of Debug Data Transfer Registers

Michal Orzel (1):
xen/arm: arm64: Add emulation of Debug Data Transfer Registers

 SUPPORT.md                           | 12 +++++
 docs/misc/xen-command-line.pandoc    | 11 +++++
 xen/arch/arm/Kconfig                 |  9 ++++
 xen/arch/arm/arm64/vsysreg.c         | 68 +++++++++++++++++++---------
 xen/arch/arm/include/asm/arm64/hsr.h |  3 ++
 xen/arch/arm/include/asm/cpregs.h    |  2 +
 xen/arch/arm/include/asm/traps.h     |  6 +++
 xen/arch/arm/traps.c                 |  9 ++++
 xen/arch/arm/vcpreg.c                | 63 ++++++++++++++++++--------
 9 files changed, 144 insertions(+), 39 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v5 1/3 xen/arm: Introduce CONFIG_PARTIAL_EMULATION and "partial-emulation" cmd option

There can be situations when the registers cannot be emulated to their full functionality. This can be due to the complexity involved. In such cases, one can emulate those registers as RAZ/WI for example. We call them as partial emulation.

Some registers are non-optional and as such there is nothing preventing an OS from accessing them.
Instead of injecting undefined exception (thus crashing a guest), one may want to prefer a partial emulation to let the guest running (in some cases accepting the fact that it might result in unwanted behavior).

A suitable example of this (as seen in subsequent patches) is emulation of DBGDTRTX_EL0 (on Arm64) and DBGDTRTXINT(on Arm32). These non-optional registers can be emulated as RAZ/WI and they can be enclosed within CONFIG_PARTIAL_EMULATION.

Further, "partial-emulation" command line option allows us to enable/disable partial emulation at run time. While CONFIG_PARTIAL_EMULATION enables support for partial emulation at compile time (i.e. adds code for partial emulation), this option may be enabled or disabled by Yocto or other build systems. However if the build system turns this option on, users can use scripts like Imagebuilder to generate uboot-script which will append "partial-emulation=true" to xen command line to turn on the partial emulation. Thus, it helps to avoid rebuilding xen.

By default, "CONFIG_PARTIAL_EMULATION=y" and "partial-emulation=false". This is done so that Xen supports partial emulation. However, customers are fully aware when they enable partial emulation. It's important to note that enabling such support might result in unwanted/non-spec compliant behavior.

Added a note in SUPPORT.md to clarify the security support for partial emulation.

---

Changes from v1 :-
1. New patch introduced in v2.

v2 :-
1. Reordered the patches so that the config and command line option is introduced in the first patch.

v3 :-
1. Defined a macro 'partial_emulation' to reduce if-defs.
2. Fixed style issues.

v4 :-
1. Added a note in SUPPORT.md.

 SUPPORT.md                        | 12 ++++++++++++
 docs/misc/xen-command-line.pandoc | 11 +++++++++++
 xen/arch/arm/Kconfig              |  9 +++++++++
 xen/arch/arm/include/asm/traps.h  |  6 ++++++
 xen/arch/arm/traps.c              |  9 +++++++++
 5 files changed, 47 insertions(+)
SUPPORT.md +12 -0
index a90d1108c9..037578b10c 100644
@@ -101,6 +101,18 @@ Extension to the GICv3 interrupt controller to support MSI.
 
     Status: Experimental
 
+### ARM/Partial Emulation
+
+Enable partial emulation of registers, otherwise considered unimplemented,
+that would normally trigger a fault injection.
+
+    Status: Supported, with caveats
+
+Bugs allowing the userspace to attack the guest OS will not be considered
+security vulnerabilities.
+
+Bugs that could compromise Xen will be considered security vulnerabilities.
+
 ### ARM Scalable Vector Extension (SVE/SVE2)
 
 Arm64 domains can use Scalable Vector Extension (SVE/SVE2).
docs/misc/xen-command-line.pandoc +11 -0
index be76be8d53..0cac601066 100644
@@ -1942,6 +1942,17 @@ This option is ignored in **pv-shim** mode.
 
 > Default: `on`
 
+### partial-emulation (arm)
+> `= <boolean>`
+
+> Default: `false`
+
+Flag to enable or disable partial emulation of system/coprocessor registers.
+Only effective if CONFIG_PARTIAL_EMULATION is enabled.
+
+**WARNING: Enabling this option might result in unwanted/non-spec compliant
+behavior.**
+
 ### pci
     = List of [ serr=<bool>, perr=<bool> ]
 
xen/arch/arm/Kconfig +9 -0
index 50e9bfae1a..dc243f4124 100644
@@ -225,6 +225,15 @@ config STATIC_EVTCHN
 	  This option enables establishing static event channel communication
 	  between domains on a dom0less system (domU-domU as well as domU-dom0).
 
+config PARTIAL_EMULATION
+	bool "Enable partial emulation of system/coprocessor registers"
+	default y
+	help
+	  This option enables partial emulation of registers to prevent guests
+	  crashing when accessing registers which are not optional but have not been
+	  emulated to their complete functionality. Enabling this might result in
+	  unwanted/non-spec compliant behavior.
+
 endmenu
 
 menu "ARM errata workaround via the alternative framework"
xen/arch/arm/include/asm/traps.h +6 -0
index 883dae368e..9a60dbf70e 100644
@@ -10,6 +10,12 @@
 # include <asm/arm64/traps.h>
 #endif
 
+#ifdef CONFIG_PARTIAL_EMULATION
+extern bool partial_emulation;
+#else
+#define partial_emulation false
+#endif
+
 /*
  * GUEST_BUG_ON is intended for checking that the guest state has not been
  * corrupted in hardware and/or that the hardware behaves as we
xen/arch/arm/traps.c +9 -0
index 9c10e8f78c..d1c7a6c516 100644
@@ -42,6 +42,15 @@
 #include <asm/vgic.h>
 #include <asm/vtimer.h>
 
+/*
+ * partial_emulation: If true, partial emulation for system/coprocessor
+ * registers will be enabled.
+ */
+#ifdef CONFIG_PARTIAL_EMULATION
+bool __ro_after_init partial_emulation = false;
+boolean_param("partial-emulation", partial_emulation);
+#endif
+
 /* The base of the stack must always be double-word aligned, which means
  * that both the kernel half of struct cpu_user_regs (which is pushed in
  * entry.S) and struct cpu_info (which lives at the bottom of a Xen
Signature · 1 line
2.25.1
#8 lore raw

PATCH v5 2/3 xen/arm: arm64: Add emulation of Debug Data Transfer Registers

In reply to Ayan Kumar Halder

From: Michal Orzel <[email protected]>

Currently, if user enables HVC_DCC config option in Linux, it invokes access to debug data transfer registers (i.e. DBGDTRTX_EL0 on arm64, DBGDTRTXINT on arm32). As these registers are not emulated, Xen injects an undefined exception to the guest and Linux crashes.

To prevent this crash, introduce a partial emulation of DBGDTR[TR]X_EL0 (these registers share the same encoding) as RAZ/WI and MDCCSR_EL0 as TXfull.

Refer ARM DDI 0487J.a ID042523, D19.3.8, DBGDTRTX_EL0 "If TXfull is set to 1, set DTRRX and DTRTX to UNKNOWN".

Thus, any OS is expected to read MDCCSR_EL0 and check for TXfull before using DBGDTRTX_EL0. Linux does it via hvc_dcc_init() ---> hvc_dcc_check(), and returns -ENODEV in case TXfull bit is still set after writing a test character. This way we prevent the guest from making use of HVC DCC as a console.

---
Changes from

v1 :- 1. DBGDTR_EL0 does not emulate RXfull. This is to avoid giving the OS any indication that the RX buffer is full and is waiting to be read.

2. In Arm32, DBGOSLSR is emulated. Also DBGDTRTXINT is emulated at EL0 only.

3. Fixed the commit message and inline code comments.

v2 :- 1. Split the patch into two (separate patches for arm64 and arm32).
2. Removed the "fail" label.
3. Fixed the commit message.

v3 :- 1. "HSR_SYSREG_MDCCSR_EL0" emulation differs based on whether partial_emulation_enabled is true or not.

2. If partial_emulation_enabled is false, then access to HSR_SYSREG_DBGDTR_EL0, HSR_SYSREG_DBGDTRTX_EL0 would lead to undefined exception.

v4 :- 1. Invoked "goto fail" from "default:" to ensure compliance with MISRA 15.3.

 xen/arch/arm/arm64/vsysreg.c         | 68 +++++++++++++++++++---------
 xen/arch/arm/include/asm/arm64/hsr.h |  3 ++
 2 files changed, 50 insertions(+), 21 deletions(-)
xen/arch/arm/arm64/vsysreg.c +47 -21
index b5d54c569b..80918bc799 100644
@@ -82,6 +82,7 @@ TVM_REG(CONTEXTIDR_EL1)
 void do_sysreg(struct cpu_user_regs *regs,
                const union hsr hsr)
 {
+    const struct hsr_sysreg sysreg = hsr.sysreg;
     int regidx = hsr.sysreg.reg;
     struct vcpu *v = current;
 
@@ -159,9 +160,6 @@ void do_sysreg(struct cpu_user_regs *regs,
      *
      * Unhandled:
      *    MDCCINT_EL1
-     *    DBGDTR_EL0
-     *    DBGDTRRX_EL0
-     *    DBGDTRTX_EL0
      *    OSDTRRX_EL1
      *    OSDTRTX_EL1
      *    OSECCR_EL1
@@ -171,12 +169,42 @@ void do_sysreg(struct cpu_user_regs *regs,
      */
     case HSR_SYSREG_MDSCR_EL1:
         return handle_raz_wi(regs, regidx, hsr.sysreg.read, hsr, 1);
+
+    /*
+     * Xen doesn't expose a real (or emulated) Debug Communications Channel
+     * (DCC) to a domain. Yet the Arm ARM implies this is not an optional
+     * feature. So some domains may start to probe it. For instance, the
+     * HVC_DCC driver in Linux (since f377775dc083 and at least up to v6.7),
+     * will try to write some characters and check if the transmit buffer
+     * has emptied.
+     */
     case HSR_SYSREG_MDCCSR_EL0:
         /*
+         * By setting TX status bit (only if partial emulation is enabled) to
+         * indicate the transmit buffer is full, we would hint the OS that the
+         * DCC is probably not working.
+         *
+         * Bit 29: TX full
+         *
          * Accessible at EL0 only if MDSCR_EL1.TDCC is set to 0. We emulate that
          * register as RAZ/WI above. So RO at both EL0 and EL1.
          */
-        return handle_ro_raz(regs, regidx, hsr.sysreg.read, hsr, 0);
+        return handle_ro_read_val(regs, regidx, hsr.sysreg.read, hsr, 0,
+                                  partial_emulation ? (1U << 29) : 0);
+
+    case HSR_SYSREG_DBGDTR_EL0:
+    /* DBGDTR[TR]X_EL0 share the same encoding */
+    case HSR_SYSREG_DBGDTRTX_EL0:
+        /*
+         * Emulate as RAZ/WI (only if partial emulation is enabled) to prevent
+         * injecting undefined exception.
+         * Accessible at EL0 only if MDSCR_EL1.TDCC is set to 0. We emulate that
+         * register as RAZ/WI.
+         */
+        if ( !partial_emulation )
+            goto fail;
+        return handle_raz_wi(regs, regidx, hsr.sysreg.read, hsr, 0);
+
     HSR_SYSREG_DBG_CASES(DBGBVR):
     HSR_SYSREG_DBG_CASES(DBGBCR):
     HSR_SYSREG_DBG_CASES(DBGWVR):
@@ -394,26 +422,24 @@ void do_sysreg(struct cpu_user_regs *regs,
      * And all other unknown registers.
      */
     default:
-        {
-            const struct hsr_sysreg sysreg = hsr.sysreg;
-
-            gdprintk(XENLOG_ERR,
-                     "%s %d, %d, c%d, c%d, %d %s x%d @ 0x%"PRIregister"\n",
-                     sysreg.read ? "mrs" : "msr",
-                     sysreg.op0, sysreg.op1,
-                     sysreg.crn, sysreg.crm,
-                     sysreg.op2,
-                     sysreg.read ? "=>" : "<=",
-                     sysreg.reg, regs->pc);
-            gdprintk(XENLOG_ERR,
-                     "unhandled 64-bit sysreg access %#"PRIregister"\n",
-                     hsr.bits & HSR_SYSREG_REGS_MASK);
-            inject_undef_exception(regs, hsr);
-            return;
-        }
+        goto fail;
     }
 
     regs->pc += 4;
+    return;
+
+ fail:
+
+    gdprintk(XENLOG_ERR,
+             "%s %d, %d, c%d, c%d, %d %s x%d @ 0x%"PRIregister"\n",
+             sysreg.read ? "mrs" : "msr", sysreg.op0, sysreg.op1, sysreg.crn,
+             sysreg.crm, sysreg.op2, sysreg.read ? "=>" : "<=", sysreg.reg,
+             regs->pc);
+    gdprintk(XENLOG_ERR,
+             "unhandled 64-bit sysreg access %#"PRIregister"\n",
+             hsr.bits & HSR_SYSREG_REGS_MASK);
+    inject_undef_exception(regs, hsr);
+    return;
 }
 
 /*
xen/arch/arm/include/asm/arm64/hsr.h +3 -0
index e691d41c17..1495ccddea 100644
@@ -47,6 +47,9 @@
 #define HSR_SYSREG_OSDLR_EL1      HSR_SYSREG(2,0,c1,c3,4)
 #define HSR_SYSREG_DBGPRCR_EL1    HSR_SYSREG(2,0,c1,c4,4)
 #define HSR_SYSREG_MDCCSR_EL0     HSR_SYSREG(2,3,c0,c1,0)
+#define HSR_SYSREG_DBGDTR_EL0     HSR_SYSREG(2,3,c0,c4,0)
+#define HSR_SYSREG_DBGDTRTX_EL0   HSR_SYSREG(2,3,c0,c5,0)
+#define HSR_SYSREG_DBGDTRRX_EL0   HSR_SYSREG(2,3,c0,c5,0)
 
 #define HSR_SYSREG_DBGBVRn_EL1(n) HSR_SYSREG(2,0,c0,c##n,4)
 #define HSR_SYSREG_DBGBCRn_EL1(n) HSR_SYSREG(2,0,c0,c##n,5)
Signature · 1 line
2.25.1
#12 lore raw

PATCH v5 3/3 xen/arm: arm32: Add emulation of Debug Data Transfer Registers

In reply to Ayan Kumar Halder

When user enables HVC_DCC config option in Linux, it invokes access to debug transfer register (i.e. DBGDTRTXINT). As this register is not emulated, Xen injects an undefined exception to the guest and Linux crashes.
To prevent this crash, introduce a partial emulation of DBGDTRTXINT as RAZ/WI and TXfull should be set to 1. So that Linux will see that TXfull is set, and it will not thereafter access DBGDTRTXINT (it will access it at least once to later check if TXfull is set).

As a pre-requisite, DBGOSLSR should be emulated in the same way as its AArch64 variant (ie OSLSR_EL1).
This is to ensure that DBGOSLSR.OSLK is 0, thus DBGDSCRINT.TXfull is treated as UNK/SBZP. DBGOSLSR.OSLM[1] is set to 1 to mantain consistency with OSLSR_EL1.

Only DBGDSCRINT can be emulated. DBGDSCRINT can be accessed at EL0 as DBGDSCREXT is emulated as RAZ (as DBGOSLSR.OSLK == 0). So, we took the opportunity to fix the minimum EL for DBGDSCRINT.DBGDSCRINT.TXfull is set to 1.

Refer ARM DDI 0487J.a ID042523, G8.3.19, DBGDTRTXint "If TXfull is set to 1, set DTRTX to UNKNOWN". So, DBGDTR[TR]XINT is emulated as RAZ/WI.

Thus, any OS is expected to read DBGDSCRINT and check for TXfull before using DBGDTRTXINT.

---
Changes from

v1 :- 1. DBGDTR_EL0 does not emulate RXfull. This is to avoid giving the OS any indication that the RX buffer is full and is waiting to be read.

2. In Arm32, DBGOSLSR is emulated. Also DBGDTRTXINT is emulated at EL0 only.

3. Fixed the commit message and inline code comments.

v2 :- 1. Split the patch into two (separate patches for arm64 and arm32).
2. Fixed in line comments and style related issues.
3. Updated commit message to mention DBGDSCRINT handling.

v3 :- 1. The original emulation of DBGDSCRINT is retained when 'partial_emulation' is false.

2. If 'partial_emulation' is false, then access to DBGDTRTXINT will lead to undefined exception.

v4 :- 1. Invoked "goto fail" from "default:" to ensure compliance with MISRA 15.3.

 xen/arch/arm/include/asm/cpregs.h |  2 +
 xen/arch/arm/vcpreg.c             | 63 ++++++++++++++++++++++---------
 2 files changed, 47 insertions(+), 18 deletions(-)
xen/arch/arm/include/asm/cpregs.h +2 -0
index 6b083de204..aec9e8f329 100644
@@ -75,6 +75,8 @@
 #define DBGDIDR         p14,0,c0,c0,0   /* Debug ID Register */
 #define DBGDSCRINT      p14,0,c0,c1,0   /* Debug Status and Control Internal */
 #define DBGDSCREXT      p14,0,c0,c2,2   /* Debug Status and Control External */
+#define DBGDTRRXINT     p14,0,c0,c5,0   /* Debug Data Transfer Register, Receive */
+#define DBGDTRTXINT     p14,0,c0,c5,0   /* Debug Data Transfer Register, Transmit */
 #define DBGVCR          p14,0,c0,c7,0   /* Vector Catch */
 #define DBGBVR0         p14,0,c0,c0,4   /* Breakpoint Value 0 */
 #define DBGBCR0         p14,0,c0,c0,5   /* Breakpoint Control 0 */
xen/arch/arm/vcpreg.c +45 -18

109 lines not on this page · raw message

Signature · 1 line
2.25.1
#2 lore raw

Re: [XEN v5 1/3] xen/arm: Introduce CONFIG_PARTIAL_EMULATION and "partial-emulation" cmd option

In reply to Ayan Kumar Halder
Quoted 25 lines · Ayan Kumar Halder

There can be situations when the registers cannot be emulated to their full functionality. This can be due to the complexity involved. In such cases, one can emulate those registers as RAZ/WI for example. We call them as partial emulation.

Some registers are non-optional and as such there is nothing preventing an OS from accessing them.
Instead of injecting undefined exception (thus crashing a guest), one may want to prefer a partial emulation to let the guest running (in some cases accepting the fact that it might result in unwanted behavior).

A suitable example of this (as seen in subsequent patches) is emulation of DBGDTRTX_EL0 (on Arm64) and DBGDTRTXINT(on Arm32). These non-optional registers can be emulated as RAZ/WI and they can be enclosed within CONFIG_PARTIAL_EMULATION.

Further, "partial-emulation" command line option allows us to enable/disable partial emulation at run time. While CONFIG_PARTIAL_EMULATION enables support for partial emulation at compile time (i.e. adds code for partial emulation), this option may be enabled or disabled by Yocto or other build systems. However if the build system turns this option on, users can use scripts like Imagebuilder to generate uboot-script which will append "partial-emulation=true" to xen command line to turn on the partial emulation. Thus, it helps to avoid rebuilding xen.

Quoted 7 lines

By default, "CONFIG_PARTIAL_EMULATION=y" and "partial-emulation=false". This is done so that Xen supports partial emulation. However, customers are fully aware when they enable partial emulation. It's important to note that enabling such support might result in unwanted/non-spec compliant behavior.

Added a note in SUPPORT.md to clarify the security support for partial emulation.

There may have been prior discussion of this, but I view this as too little to justify ...

Quoted 7 lines
SUPPORT.md +1 -0
@@ -101,6 +101,18 @@ Extension to the GICv3 interrupt controller to support MSI.
 
     Status: Experimental
 
+### ARM/Partial Emulation

SUPPORT.md Extension to the GICv3 interrupt controller to support MSI. · line 114

Quoted 10 lines
SUPPORT.md +10 -0
+
+Enable partial emulation of registers, otherwise considered unimplemented,
+that would normally trigger a fault injection.
+
+    Status: Supported, with caveats
+
+Bugs allowing the userspace to attack the guest OS will not be considered
+security vulnerabilities.
+
+Bugs that could compromise Xen will be considered security vulnerabilities.

... the odd statement regarding in-guest vulnerabilities that might be introduced. I see only two ways of treating this as supported: Either you simply refuse emulation when the access is from user space, or you support that mode of emulation as much as that of kernel space accesses. After all "injecting undefined exception (thus crashing a guest)" (quoting the description) doesn't seem quite right: If you inject such an exception for a user space access, all that ought to happen is the user space process to be killed.

Jan

#3 lore raw

Re: [XEN v5 1/3] xen/arm: Introduce CONFIG_PARTIAL_EMULATION and "partial-emulation" cmd option

Hi Jan,

Quoted 42 lines · Jan Beulich
Quoted 25 lines · Ayan Kumar Halder

There can be situations when the registers cannot be emulated to their full functionality. This can be due to the complexity involved. In such cases, one can emulate those registers as RAZ/WI for example. We call them as partial emulation.

Some registers are non-optional and as such there is nothing preventing an OS from accessing them.
Instead of injecting undefined exception (thus crashing a guest), one may want to prefer a partial emulation to let the guest running (in some cases accepting the fact that it might result in unwanted behavior).

A suitable example of this (as seen in subsequent patches) is emulation of DBGDTRTX_EL0 (on Arm64) and DBGDTRTXINT(on Arm32). These non-optional registers can be emulated as RAZ/WI and they can be enclosed within CONFIG_PARTIAL_EMULATION.

Further, "partial-emulation" command line option allows us to enable/disable partial emulation at run time. While CONFIG_PARTIAL_EMULATION enables support for partial emulation at compile time (i.e. adds code for partial emulation), this option may be enabled or disabled by Yocto or other build systems. However if the build system turns this option on, users can use scripts like Imagebuilder to generate uboot-script which will append "partial-emulation=true" to xen command line to turn on the partial emulation. Thus, it helps to avoid rebuilding xen.

Quoted 7 lines

By default, "CONFIG_PARTIAL_EMULATION=y" and "partial-emulation=false". This is done so that Xen supports partial emulation. However, customers are fully aware when they enable partial emulation. It's important to note that enabling such support might result in unwanted/non-spec compliant behavior.

Added a note in SUPPORT.md to clarify the security support for partial emulation.

There may have been prior discussion of this, but I view this as too little to justify ...

Quoted 6 lines
SUPPORT.md +0 -0
@@ -101,6 +101,18 @@ Extension to the GICv3 interrupt controller to support MSI.
 
      Status: Experimental
 
Quoted 14 lines

SUPPORT.md Extension to the GICv3 interrupt controller to support MSI. · line 114

Quoted 11 lines
SUPPORT.md +11 -0
+### ARM/Partial Emulation
+
+Enable partial emulation of registers, otherwise considered unimplemented,
+that would normally trigger a fault injection.
+
+    Status: Supported, with caveats
+
+Bugs allowing the userspace to attack the guest OS will not be considered
+security vulnerabilities.
+
+Bugs that could compromise Xen will be considered security vulnerabilities.

... the odd statement regarding in-guest vulnerabilities that might be introduced. I see only two ways of treating this as supported: Either you simply refuse emulation when the access is from user space,

I am wondering how do we enforce that.

Let me try to understand this with the current implementation of partial emulation for system registers.

1. DBGDTRTX_EL0 :- I understand that EL0 access to this register will cause a trap to EL2. The reason being MDCR_EL2.TDA == 1.

In that case, if we refuse emulation then an undef exception is injected to the guest (this is the behavior as of today even without this patch).

So, are you saying that the undef exception is to be injected to the user space process. This may be possible for Arm64 guests (inject_undef64_exception() needs to be changed).

However for Arm32 guests, this may not be possible as the mode changes to PSR_MODE_UND.

Let me know if I understood you correctly.

Quoted 2 lines

or you
support that mode of emulation as much as that of kernel space accesses.

Do you mean we support partial emulation only for traps from EL1 mode ?

- Ayan

Quoted 6 lines

After all "injecting undefined exception (thus crashing a guest)" (quoting the description) doesn't seem quite right: If you inject such an exception for a user space access, all that ought to happen is the user space process to be killed.

Jan

#4 lore raw

Re: [XEN v5 1/3] xen/arm: Introduce CONFIG_PARTIAL_EMULATION and "partial-emulation" cmd option

Quoted 31 lines · Ayan Kumar Halder
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Ayan Kumar Halder
SUPPORT.md +0 -0
@@ -101,6 +101,18 @@ Extension to the GICv3 interrupt controller to support MSI.
 
      Status: Experimental
 
Quoted 14 lines

SUPPORT.md Extension to the GICv3 interrupt controller to support MSI. · line 114

Quoted 11 lines
SUPPORT.md +11 -0
+### ARM/Partial Emulation
+
+Enable partial emulation of registers, otherwise considered unimplemented,
+that would normally trigger a fault injection.
+
+    Status: Supported, with caveats
+
+Bugs allowing the userspace to attack the guest OS will not be considered
+security vulnerabilities.
+
+Bugs that could compromise Xen will be considered security vulnerabilities.

... the odd statement regarding in-guest vulnerabilities that might be introduced. I see only two ways of treating this as supported: Either you simply refuse emulation when the access is from user space,

I am wondering how do we enforce that.

Let me try to understand this with the current implementation of partial emulation for system registers.

1. DBGDTRTX_EL0 :- I understand that EL0 access to this register will cause a trap to EL2. The reason being MDCR_EL2.TDA == 1.

Quoted 6 lines

In that case, if we refuse emulation then an undef exception is injected to the guest (this is the behavior as of today even without this patch).

So, are you saying that the undef exception is to be injected to the user space process. This may be possible for Arm64 guests (inject_undef64_exception() needs to be changed).

No, injection is always to the guest, not to a specific entity within the guest. That ought to be the same on bare hardware: An exception when raised has an architecturally defined entry point for handling. That'll typically be kernel code. The handler then figures out whether the source of the exception was in user or kernel mode. For user mode code, the kernel may or may not try to handle the exception and then continue the user mode process. If it can't or doesn't want to handle it, it'll raise (in UNIX terms) a signal to the process. That signal, in turn, may or may not be fatal to the process. But such an exception from user mode should never be fatal to the guest as a whole.

Quoted 2 lines

However for Arm32 guests, this may not be possible as the mode changes to PSR_MODE_UND.

I'm afraid my Arm foo isn't good enough to understand this. On the surface it looks to violate above outlined principle.

Quoted 6 lines

Let me know if I understood you correctly.

Quoted 2 lines

or you
support that mode of emulation as much as that of kernel space accesses.

Do you mean we support partial emulation only for traps from EL1 mode ?

Possibly.

Jan

#5 lore raw

Re: [XEN v5 1/3] xen/arm: Introduce CONFIG_PARTIAL_EMULATION and "partial-emulation" cmd option

Hi Jan,

Quoted 37 lines · Jan Beulich
Quoted 30 lines · Ayan Kumar Halder
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Ayan Kumar Halder
SUPPORT.md +0 -0
@@ -101,6 +101,18 @@ Extension to the GICv3 interrupt controller to support MSI.
 
       Status: Experimental
 
Quoted 14 lines

SUPPORT.md Extension to the GICv3 interrupt controller to support MSI. · line 114

Quoted 11 lines
SUPPORT.md +11 -0
+### ARM/Partial Emulation
+
+Enable partial emulation of registers, otherwise considered unimplemented,
+that would normally trigger a fault injection.
+
+    Status: Supported, with caveats
+
+Bugs allowing the userspace to attack the guest OS will not be considered
+security vulnerabilities.
+
+Bugs that could compromise Xen will be considered security vulnerabilities.

... the odd statement regarding in-guest vulnerabilities that might be introduced. I see only two ways of treating this as supported: Either you simply refuse emulation when the access is from user space,

I am wondering how do we enforce that.

Let me try to understand this with the current implementation of partial emulation for system registers.

1. DBGDTRTX_EL0 :- I understand that EL0 access to this register will cause a trap to EL2. The reason being MDCR_EL2.TDA == 1.

Quoted 6 lines

In that case, if we refuse emulation then an undef exception is injected to the guest (this is the behavior as of today even without this patch).

So, are you saying that the undef exception is to be injected to the user space process. This may be possible for Arm64 guests (inject_undef64_exception() needs to be changed).

Quoted 10 lines

No, injection is always to the guest, not to a specific entity within the guest. That ought to be the same on bare hardware: An exception when raised has an architecturally defined entry point for handling. That'll typically be kernel code. The handler then figures out whether the source of the exception was in user or kernel mode. For user mode code, the kernel may or may not try to handle the exception and then continue the user mode process. If it can't or doesn't want to handle it, it'll raise (in UNIX terms) a signal to the process. That signal, in turn, may or may not be fatal to the process. But such an exception from user mode should never be fatal to the guest as a whole.

Thanks for explaining it so well.

Quoted 6 lines
Quoted 3 lines

However for Arm32 guests, this may not be possible as the mode changes to PSR_MODE_UND.

I'm afraid my Arm foo isn't good enough to understand this. On the surface it looks to violate above outlined principle.

Quoted 6 lines
Quoted 5 lines

Let me know if I understood you correctly.

Quoted 2 lines

or you
support that mode of emulation as much as that of kernel space accesses.

Do you mean we support partial emulation only for traps from EL1 mode ?

Possibly.

Now, I understand you. We will allow partial_emulation only from kernel mode.

So we need to do sth :-

if ( 'partial_emulation == true' && '!regs_mode_is_user(regs)' )

{

     /* partial_emulation logic */

}

I am ok with this.

And the updates message will be

### ARM/Partial Emulation

Enable partial emulation of registers, otherwise considered unimplemented, that would normally trigger a fault injection.

Status: Supported, with caveats

Partial emulation for traps from userspace is not allowed.

Bugs that could compromise Xen will be considered security vulnerabilities.

Also, want @Julien, @Michal to comment on this.

- Ayan

Quoted 2 lines

Jan

#6 lore raw

Re: [XEN v5 1/3] xen/arm: Introduce CONFIG_PARTIAL_EMULATION and "partial-emulation" cmd option

Quoted 72 lines · Ayan Kumar Halder

Hi Jan,

Quoted 39 lines · Jan Beulich
Quoted 32 lines · Ayan Kumar Halder
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Ayan Kumar Halder
SUPPORT.md +0 -0
@@ -101,6 +101,18 @@ Extension to the GICv3 interrupt controller to 

support MSI.
       Status: Experimental

SUPPORT.md +3 -0
+### ARM/Partial Emulation
+
+Enable partial emulation of registers, otherwise considered 

unimplemented,

SUPPORT.md +5 -0
+that would normally trigger a fault injection.
+
+    Status: Supported, with caveats
+
+Bugs allowing the userspace to attack the guest OS will not be 
Quoted 8 lines
Quoted 5 lines

considered

SUPPORT.md +3 -0
+security vulnerabilities.
+
+Bugs that could compromise Xen will be considered security 

vulnerabilities.

... the odd statement regarding in-guest vulnerabilities that might be introduced. I see only two ways of treating this as supported: Either you simply refuse emulation when the access is from user space,

I am wondering how do we enforce that.

Let me try to understand this with the current implementation of partial emulation for system registers.

1. DBGDTRTX_EL0 :- I understand that EL0 access to this register will cause a trap to EL2. The reason being MDCR_EL2.TDA == 1.

Quoted 6 lines

In that case, if we refuse emulation then an undef exception is injected to the guest (this is the behavior as of today even without this patch).

So, are you saying that the undef exception is to be injected to the user space process. This may be possible for Arm64 guests (inject_undef64_exception() needs to be changed).

Quoted 10 lines

No, injection is always to the guest, not to a specific entity within the guest. That ought to be the same on bare hardware: An exception when raised has an architecturally defined entry point for handling. That'll typically be kernel code. The handler then figures out whether the source of the exception was in user or kernel mode. For user mode code, the kernel may or may not try to handle the exception and then continue the user mode process. If it can't or doesn't want to handle it, it'll raise (in UNIX terms) a signal to the process. That signal, in turn, may or may not be fatal to the process. But such an exception from user mode should never be fatal to the guest as a whole.

Thanks for explaining it so well.

Quoted 6 lines
Quoted 3 lines

However for Arm32 guests, this may not be possible as the mode changes to PSR_MODE_UND.

I'm afraid my Arm foo isn't good enough to understand this. On the surface it looks to violate above outlined principle.

Quoted 6 lines
Quoted 5 lines

Let me know if I understood you correctly.

Quoted 2 lines

or you
support that mode of emulation as much as that of kernel space accesses.

Do you mean we support partial emulation only for traps from EL1 mode ?

Possibly.

Now, I understand you. We will allow partial_emulation only from kernel mode.

So we need to do sth :-

if ( 'partial_emulation == true' && '!regs_mode_is_user(regs)' )

Quoted 7 lines

{

     /* partial_emulation logic */

}

I am ok with this.

The helpers take a min_el. So you can have simpler code and set the value to 1 (rather than 0 today).

Quoted 11 lines

And the updates message will be

### ARM/Partial Emulation

Enable partial emulation of registers, otherwise considered unimplemented, that would normally trigger a fault injection.

    Status: Supported, with caveats

Partial emulation for traps from userspace is not allowed.

I am not sure how this helps...

Quoted 2 lines

Bugs that could compromise Xen will be considered security vulnerabilities.

... you are still implying that any userland breaching the kernel will not be supported because of a bug.

But, I don't see how preventing the userland to access a register will help. In theory you could have a register that can only be accessed by EL1 but has an impact to EL0.

By definition, it means that we don't faithfully follow the Arm Arm and anything can happen. Which is why I wanted to exclude security support from userland and kernel.

I think the current registers are low risk. But I am under the impression that you may wan to add more partial emulation. So we need to make some sort of statement that doesn't put any burden to the security team for future registers.

An option would be to explicitly list the registers in SUPPORT.md. As I mentioned above, I think the registers you currently emulate are low-risk to introduce a security bug. So I would be ok to fully security support them. This would need to be re-assessed for new registers.

I am open to other suggestions.

Cheers,

Signature · 1 line
Julien Grall
#7 lore raw

Re: [XEN v5 1/3] xen/arm: Introduce CONFIG_PARTIAL_EMULATION and "partial-emulation" cmd option

Quoted 121 lines · Julien Grall
Quoted 75 lines · Ayan Kumar Halder

Hi Jan,

Quoted 39 lines · Jan Beulich
Quoted 32 lines · Ayan Kumar Halder
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Ayan Kumar Halder
SUPPORT.md +0 -0
@@ -101,6 +101,18 @@ Extension to the GICv3 interrupt controller to

support MSI.
Status: Experimental

SUPPORT.md +3 -0
+### ARM/Partial Emulation
+
+Enable partial emulation of registers, otherwise considered

unimplemented,

SUPPORT.md +5 -0
+that would normally trigger a fault injection.
+
+    Status: Supported, with caveats
+
+Bugs allowing the userspace to attack the guest OS will not be
Quoted 8 lines
Quoted 5 lines

considered

SUPPORT.md +3 -0
+security vulnerabilities.
+
+Bugs that could compromise Xen will be considered security

vulnerabilities.

... the odd statement regarding in-guest vulnerabilities that might be introduced. I see only two ways of treating this as supported: Either you simply refuse emulation when the access is from user space,

I am wondering how do we enforce that.

Let me try to understand this with the current implementation of partial emulation for system registers.

1. DBGDTRTX_EL0 :- I understand that EL0 access to this register will cause a trap to EL2. The reason being MDCR_EL2.TDA == 1.

Quoted 6 lines

In that case, if we refuse emulation then an undef exception is injected to the guest (this is the behavior as of today even without this patch).

So, are you saying that the undef exception is to be injected to the user space process. This may be possible for Arm64 guests (inject_undef64_exception() needs to be changed).

Quoted 10 lines

No, injection is always to the guest, not to a specific entity within the guest. That ought to be the same on bare hardware: An exception when raised has an architecturally defined entry point for handling. That'll typically be kernel code. The handler then figures out whether the source of the exception was in user or kernel mode. For user mode code, the kernel may or may not try to handle the exception and then continue the user mode process. If it can't or doesn't want to handle it, it'll raise (in UNIX terms) a signal to the process. That signal, in turn, may or may not be fatal to the process. But such an exception from user mode should never be fatal to the guest as a whole.

Thanks for explaining it so well.

Quoted 7 lines
Quoted 3 lines

However for Arm32 guests, this may not be possible as the mode changes to PSR_MODE_UND.

I'm afraid my Arm foo isn't good enough to understand this. On the surface
it looks to violate above outlined principle.

Quoted 7 lines
Quoted 6 lines

Let me know if I understood you correctly.

Quoted 3 lines

or you
support that mode of emulation as much as that of kernel space accesses.

Do you mean we support partial emulation only for traps from EL1 mode ?

Possibly.

Now, I understand you. We will allow partial_emulation only from kernel mode.

So we need to do sth :-

if ( 'partial_emulation == true' && '!regs_mode_is_user(regs)' )

Quoted 7 lines

{

      /* partial_emulation logic */

}

I am ok with this.

The helpers take a min_el. So you can have simpler code and set the value to 1 (rather than 0 today).

Quoted 11 lines

And the updates message will be

### ARM/Partial Emulation

Enable partial emulation of registers, otherwise considered unimplemented, that would normally trigger a fault injection.

Status: Supported, with caveats

Partial emulation for traps from userspace is not allowed.

I am not sure how this helps...

Quoted 2 lines

Bugs that could compromise Xen will be considered security vulnerabilities.

... you are still implying that any userland breaching the kernel will not be supported because of a bug.

But, I don't see how preventing the userland to access a register will help. In theory you could have a register that can only be accessed by EL1 but has an impact to EL0.

By definition, it means that we don't faithfully follow the Arm Arm and anything can happen. Which is why I wanted to exclude security support from userland and kernel.

I think the current registers are low risk. But I am under the impression that you may wan to add more partial emulation. So we need to make some sort of statement that doesn't put any burden to the security team for future registers.

Quoted 6 lines

An option would be to explicitly list the registers in SUPPORT.md. As I mentioned above, I think the registers you currently emulate are low-risk to introduce a security bug. So I would be ok to fully security support them. This would need to be re-assessed for new registers.

I am open to other suggestions.

+1. Providing partial emulation support only for kernel mode where the Arm ARM allows for user mode access is confusing and would require excessive number of comments to back up the implementation. While it might be ok for DCC, imposing such limit on all the future use cases sounds too severe.

I think the following would be ok:

Status: Supported, with caveats

Only the following system registers are security supported:
- ...

~Michal

#9 lore raw

Re: [XEN v5 2/3] xen/arm: arm64: Add emulation of Debug Data Transfer Registers

In reply to Ayan Kumar Halder

Hi,

Quoted 144 lines · Ayan Kumar Halder

From: Michal Orzel <[email protected]>

Currently, if user enables HVC_DCC config option in Linux, it invokes access to debug data transfer registers (i.e. DBGDTRTX_EL0 on arm64, DBGDTRTXINT on arm32). As these registers are not emulated, Xen injects an undefined exception to the guest and Linux crashes.

To prevent this crash, introduce a partial emulation of DBGDTR[TR]X_EL0 (these registers share the same encoding) as RAZ/WI and MDCCSR_EL0 as TXfull.

Refer ARM DDI 0487J.a ID042523, D19.3.8, DBGDTRTX_EL0 "If TXfull is set to 1, set DTRRX and DTRTX to UNKNOWN".

Thus, any OS is expected to read MDCCSR_EL0 and check for TXfull before using DBGDTRTX_EL0. Linux does it via hvc_dcc_init() ---> hvc_dcc_check(), and returns -ENODEV in case TXfull bit is still set after writing a test character. This way we prevent the guest from making use of HVC DCC as a console.

---
Changes from

v1 :- 1. DBGDTR_EL0 does not emulate RXfull. This is to avoid giving the OS any indication that the RX buffer is full and is waiting to be read.

2. In Arm32, DBGOSLSR is emulated. Also DBGDTRTXINT is emulated at EL0 only.

3. Fixed the commit message and inline code comments.

v2 :- 1. Split the patch into two (separate patches for arm64 and arm32).
2. Removed the "fail" label.
3. Fixed the commit message.

v3 :- 1. "HSR_SYSREG_MDCCSR_EL0" emulation differs based on whether partial_emulation_enabled is true or not.

2. If partial_emulation_enabled is false, then access to HSR_SYSREG_DBGDTR_EL0, HSR_SYSREG_DBGDTRTX_EL0 would lead to undefined exception.

v4 :- 1. Invoked "goto fail" from "default:" to ensure compliance with MISRA 15.3.

  xen/arch/arm/arm64/vsysreg.c         | 68 +++++++++++++++++++---------
  xen/arch/arm/include/asm/arm64/hsr.h |  3 ++
  2 files changed, 50 insertions(+), 21 deletions(-)
xen/arch/arm/arm64/vsysreg.c +37 -21
index b5d54c569b..80918bc799 100644
@@ -82,6 +82,7 @@ TVM_REG(CONTEXTIDR_EL1)
  void do_sysreg(struct cpu_user_regs *regs,
                 const union hsr hsr)
  {
+    const struct hsr_sysreg sysreg = hsr.sysreg;
      int regidx = hsr.sysreg.reg;
      struct vcpu *v = current;
 
@@ -159,9 +160,6 @@ void do_sysreg(struct cpu_user_regs *regs,
       *
       * Unhandled:
       *    MDCCINT_EL1
-     *    DBGDTR_EL0
-     *    DBGDTRRX_EL0
-     *    DBGDTRTX_EL0
       *    OSDTRRX_EL1
       *    OSDTRTX_EL1
       *    OSECCR_EL1
@@ -171,12 +169,42 @@ void do_sysreg(struct cpu_user_regs *regs,
       */
      case HSR_SYSREG_MDSCR_EL1:
          return handle_raz_wi(regs, regidx, hsr.sysreg.read, hsr, 1);
+
+    /*
+     * Xen doesn't expose a real (or emulated) Debug Communications Channel
+     * (DCC) to a domain. Yet the Arm ARM implies this is not an optional
+     * feature. So some domains may start to probe it. For instance, the
+     * HVC_DCC driver in Linux (since f377775dc083 and at least up to v6.7),
+     * will try to write some characters and check if the transmit buffer
+     * has emptied.
+     */
      case HSR_SYSREG_MDCCSR_EL0:
          /*
+         * By setting TX status bit (only if partial emulation is enabled) to
+         * indicate the transmit buffer is full, we would hint the OS that the
+         * DCC is probably not working.
+         *
+         * Bit 29: TX full
+         *
           * Accessible at EL0 only if MDSCR_EL1.TDCC is set to 0. We emulate that
           * register as RAZ/WI above. So RO at both EL0 and EL1.
           */
-        return handle_ro_raz(regs, regidx, hsr.sysreg.read, hsr, 0);
+        return handle_ro_read_val(regs, regidx, hsr.sysreg.read, hsr, 0,
+                                  partial_emulation ? (1U << 29) : 0);
+
+    case HSR_SYSREG_DBGDTR_EL0:
+    /* DBGDTR[TR]X_EL0 share the same encoding */
+    case HSR_SYSREG_DBGDTRTX_EL0:
+        /*
+         * Emulate as RAZ/WI (only if partial emulation is enabled) to prevent
+         * injecting undefined exception.
+         * Accessible at EL0 only if MDSCR_EL1.TDCC is set to 0. We emulate that
+         * register as RAZ/WI.
+         */
+        if ( !partial_emulation )
+            goto fail;
+        return handle_raz_wi(regs, regidx, hsr.sysreg.read, hsr, 0);
+
      HSR_SYSREG_DBG_CASES(DBGBVR):
      HSR_SYSREG_DBG_CASES(DBGBCR):
      HSR_SYSREG_DBG_CASES(DBGWVR):
@@ -394,26 +422,24 @@ void do_sysreg(struct cpu_user_regs *regs,
       * And all other unknown registers.
       */
      default:
-        {
-            const struct hsr_sysreg sysreg = hsr.sysreg;
-
-            gdprintk(XENLOG_ERR,
-                     "%s %d, %d, c%d, c%d, %d %s x%d @ 0x%"PRIregister"\n",
-                     sysreg.read ? "mrs" : "msr",
-                     sysreg.op0, sysreg.op1,
-                     sysreg.crn, sysreg.crm,
-                     sysreg.op2,
-                     sysreg.read ? "=>" : "<=",
-                     sysreg.reg, regs->pc);
-            gdprintk(XENLOG_ERR,
-                     "unhandled 64-bit sysreg access %#"PRIregister"\n",
-                     hsr.bits & HSR_SYSREG_REGS_MASK);
-            inject_undef_exception(regs, hsr);
-            return;
-        }
+        goto fail;
      }
 
      regs->pc += 4;
+    return;
+
+ fail:
+

xen/arch/arm/arm64/vsysreg.c void do_sysreg(struct cpu_user_regs *regs, · line 442

Quoted 10 lines
xen/arch/arm/arm64/vsysreg.c +10 -0
+    gdprintk(XENLOG_ERR,
+             "%s %d, %d, c%d, c%d, %d %s x%d @ 0x%"PRIregister"\n",
+             sysreg.read ? "mrs" : "msr", sysreg.op0, sysreg.op1, sysreg.crn,
+             sysreg.crm, sysreg.op2, sysreg.read ? "=>" : "<=", sysreg.reg,
+             regs->pc);
+    gdprintk(XENLOG_ERR,
+             "unhandled 64-bit sysreg access %#"PRIregister"\n",
+             hsr.bits & HSR_SYSREG_REGS_MASK);
+    inject_undef_exception(regs, hsr);
+    return;

The last 'return' needs to be removed (spotted by Michal)

- Ayan

#10 lore raw

Re: [XEN v5 2/3] xen/arm: arm64: Add emulation of Debug Data Transfer Registers

Hi Ayan,

Quoted 142 lines · Ayan Kumar Halder

Hi,

Quoted 139 lines · Ayan Kumar Halder

From: Michal Orzel <[email protected]>

Currently, if user enables HVC_DCC config option in Linux, it invokes access to debug data transfer registers (i.e. DBGDTRTX_EL0 on arm64, DBGDTRTXINT on arm32). As these registers are not emulated, Xen injects an undefined exception to the guest and Linux crashes.

To prevent this crash, introduce a partial emulation of DBGDTR[TR]X_EL0 (these registers share the same encoding) as RAZ/WI and MDCCSR_EL0 as TXfull.

Refer ARM DDI 0487J.a ID042523, D19.3.8, DBGDTRTX_EL0 "If TXfull is set to 1, set DTRRX and DTRTX to UNKNOWN".

Thus, any OS is expected to read MDCCSR_EL0 and check for TXfull before using DBGDTRTX_EL0. Linux does it via hvc_dcc_init() ---> hvc_dcc_check(), and returns -ENODEV in case TXfull bit is still set after writing a test character. This way we prevent the guest from making use of HVC DCC as a console.

---
Changes from

v1 :- 1. DBGDTR_EL0 does not emulate RXfull. This is to avoid giving the OS any indication that the RX buffer is full and is waiting to be read.

2. In Arm32, DBGOSLSR is emulated. Also DBGDTRTXINT is emulated at EL0 only.

3. Fixed the commit message and inline code comments.

v2 :- 1. Split the patch into two (separate patches for arm64 and arm32).
2. Removed the "fail" label.
3. Fixed the commit message.

v3 :- 1. "HSR_SYSREG_MDCCSR_EL0" emulation differs based on whether partial_emulation_enabled is true or not.

2. If partial_emulation_enabled is false, then access to HSR_SYSREG_DBGDTR_EL0, HSR_SYSREG_DBGDTRTX_EL0 would lead to undefined exception.

v4 :- 1. Invoked "goto fail" from "default:" to ensure compliance with MISRA 15.3.

  xen/arch/arm/arm64/vsysreg.c         | 68 +++++++++++++++++++---------
  xen/arch/arm/include/asm/arm64/hsr.h |  3 ++
  2 files changed, 50 insertions(+), 21 deletions(-)
xen/arch/arm/arm64/vsysreg.c +33 -21
index b5d54c569b..80918bc799 100644
@@ -82,6 +82,7 @@ TVM_REG(CONTEXTIDR_EL1)
  void do_sysreg(struct cpu_user_regs *regs,
                 const union hsr hsr)
  {
+    const struct hsr_sysreg sysreg = hsr.sysreg;
      int regidx = hsr.sysreg.reg;
      struct vcpu *v = current;
 
@@ -159,9 +160,6 @@ void do_sysreg(struct cpu_user_regs *regs,
       *
       * Unhandled:
       *    MDCCINT_EL1
-     *    DBGDTR_EL0
-     *    DBGDTRRX_EL0
-     *    DBGDTRTX_EL0
       *    OSDTRRX_EL1
       *    OSDTRTX_EL1
       *    OSECCR_EL1
@@ -171,12 +169,42 @@ void do_sysreg(struct cpu_user_regs *regs,
       */
      case HSR_SYSREG_MDSCR_EL1:
          return handle_raz_wi(regs, regidx, hsr.sysreg.read, hsr, 1);
+
+    /*
+     * Xen doesn't expose a real (or emulated) Debug Communications Channel
+     * (DCC) to a domain. Yet the Arm ARM implies this is not an optional
+     * feature. So some domains may start to probe it. For instance, the
+     * HVC_DCC driver in Linux (since f377775dc083 and at least up to v6.7),
+     * will try to write some characters and check if the transmit buffer
+     * has emptied.
+     */
      case HSR_SYSREG_MDCCSR_EL0:
          /*
+         * By setting TX status bit (only if partial emulation is enabled) to
+         * indicate the transmit buffer is full, we would hint the OS that the
+         * DCC is probably not working.
+         *
+         * Bit 29: TX full
+         *
           * Accessible at EL0 only if MDSCR_EL1.TDCC is set to 0. We emulate that
           * register as RAZ/WI above. So RO at both EL0 and EL1.
           */
-        return handle_ro_raz(regs, regidx, hsr.sysreg.read, hsr, 0);
+        return handle_ro_read_val(regs, regidx, hsr.sysreg.read, hsr, 0,
+                                  partial_emulation ? (1U << 29) : 0);
+
+    case HSR_SYSREG_DBGDTR_EL0:
+    /* DBGDTR[TR]X_EL0 share the same encoding */
+    case HSR_SYSREG_DBGDTRTX_EL0:
+        /*
+         * Emulate as RAZ/WI (only if partial emulation is enabled) to prevent
+         * injecting undefined exception.
+         * Accessible at EL0 only if MDSCR_EL1.TDCC is set to 0. We emulate that
+         * register as RAZ/WI.
+         */
+        if ( !partial_emulation )
+            goto fail;
+        return handle_raz_wi(regs, regidx, hsr.sysreg.read, hsr, 0);
+
      HSR_SYSREG_DBG_CASES(DBGBVR):
      HSR_SYSREG_DBG_CASES(DBGBCR):
      HSR_SYSREG_DBG_CASES(DBGWVR):
@@ -394,26 +422,24 @@ void do_sysreg(struct cpu_user_regs *regs,
       * And all other unknown registers.
       */
      default:
-        {
-            const struct hsr_sysreg sysreg = hsr.sysreg;
-
-            gdprintk(XENLOG_ERR,
-                     "%s %d, %d, c%d, c%d, %d %s x%d @ 0x%"PRIregister"\n",
-                     sysreg.read ? "mrs" : "msr",
-                     sysreg.op0, sysreg.op1,
-                     sysreg.crn, sysreg.crm,
-                     sysreg.op2,
-                     sysreg.read ? "=>" : "<=",
-                     sysreg.reg, regs->pc);
-            gdprintk(XENLOG_ERR,
-                     "unhandled 64-bit sysreg access %#"PRIregister"\n",
-                     hsr.bits & HSR_SYSREG_REGS_MASK);
-            inject_undef_exception(regs, hsr);
-            return;
-        }
+        goto fail;
      }
 
Quoted 5 lines

xen/arch/arm/arm64/vsysreg.c void do_sysreg(struct cpu_user_regs *regs, · line 432

Quoted 5 lines
xen/arch/arm/arm64/vsysreg.c +4 -0
      regs->pc += 4;
+    return;
+
+ fail:
+

No need for this empty line.

Quoted 5 lines

xen/arch/arm/arm64/vsysreg.c void do_sysreg(struct cpu_user_regs *regs, · line 437

Quoted 5 lines
xen/arch/arm/arm64/vsysreg.c +5 -0
+    gdprintk(XENLOG_ERR,
+             "%s %d, %d, c%d, c%d, %d %s x%d @ 0x%"PRIregister"\n",
+             sysreg.read ? "mrs" : "msr", sysreg.op0, sysreg.op1, sysreg.crn,
+             sysreg.crm, sysreg.op2, sysreg.read ? "=>" : "<=", sysreg.reg,
+             regs->pc);

The original formatting (i.e. placement of printk args) looked better. I'm not sure why you changed it.

Quoted 8 lines

xen/arch/arm/arm64/vsysreg.c void do_sysreg(struct cpu_user_regs *regs, · line 442

Quoted 5 lines
xen/arch/arm/arm64/vsysreg.c +5 -0
+    gdprintk(XENLOG_ERR,
+             "unhandled 64-bit sysreg access %#"PRIregister"\n",
+             hsr.bits & HSR_SYSREG_REGS_MASK);
+    inject_undef_exception(regs, hsr);
+    return;

The last 'return' needs to be removed (spotted by Michal)

~Michal

#11 lore raw

Re: [XEN v5 2/3] xen/arm: arm64: Add emulation of Debug Data Transfer Registers

Quoted 1 line · Michal Orzel

Hi Ayan,

Hi,

Quoted 144 lines · Ayan Kumar Halder
Quoted 142 lines

Hi,

Quoted 139 lines · Ayan Kumar Halder

From: Michal Orzel <[email protected]>

Currently, if user enables HVC_DCC config option in Linux, it invokes access to debug data transfer registers (i.e. DBGDTRTX_EL0 on arm64, DBGDTRTXINT on arm32). As these registers are not emulated, Xen injects an undefined exception to the guest and Linux crashes.

To prevent this crash, introduce a partial emulation of DBGDTR[TR]X_EL0 (these registers share the same encoding) as RAZ/WI and MDCCSR_EL0 as TXfull.

Refer ARM DDI 0487J.a ID042523, D19.3.8, DBGDTRTX_EL0 "If TXfull is set to 1, set DTRRX and DTRTX to UNKNOWN".

Thus, any OS is expected to read MDCCSR_EL0 and check for TXfull before using DBGDTRTX_EL0. Linux does it via hvc_dcc_init() ---> hvc_dcc_check(), and returns -ENODEV in case TXfull bit is still set after writing a test character. This way we prevent the guest from making use of HVC DCC as a console.

---
Changes from

v1 :- 1. DBGDTR_EL0 does not emulate RXfull. This is to avoid giving the OS any indication that the RX buffer is full and is waiting to be read.

2. In Arm32, DBGOSLSR is emulated. Also DBGDTRTXINT is emulated at EL0 only.

3. Fixed the commit message and inline code comments.

v2 :- 1. Split the patch into two (separate patches for arm64 and arm32).
2. Removed the "fail" label.
3. Fixed the commit message.

v3 :- 1. "HSR_SYSREG_MDCCSR_EL0" emulation differs based on whether partial_emulation_enabled is true or not.

2. If partial_emulation_enabled is false, then access to HSR_SYSREG_DBGDTR_EL0, HSR_SYSREG_DBGDTRTX_EL0 would lead to undefined exception.

v4 :- 1. Invoked "goto fail" from "default:" to ensure compliance with MISRA 15.3.

   xen/arch/arm/arm64/vsysreg.c         | 68 +++++++++++++++++++---------
   xen/arch/arm/include/asm/arm64/hsr.h |  3 ++
   2 files changed, 50 insertions(+), 21 deletions(-)
xen/arch/arm/arm64/vsysreg.c +33 -21
index b5d54c569b..80918bc799 100644
@@ -82,6 +82,7 @@ TVM_REG(CONTEXTIDR_EL1)
   void do_sysreg(struct cpu_user_regs *regs,
                  const union hsr hsr)
   {
+    const struct hsr_sysreg sysreg = hsr.sysreg;
       int regidx = hsr.sysreg.reg;
       struct vcpu *v = current;
 
@@ -159,9 +160,6 @@ void do_sysreg(struct cpu_user_regs *regs,
        *
        * Unhandled:
        *    MDCCINT_EL1
-     *    DBGDTR_EL0
-     *    DBGDTRRX_EL0
-     *    DBGDTRTX_EL0
        *    OSDTRRX_EL1
        *    OSDTRTX_EL1
        *    OSECCR_EL1
@@ -171,12 +169,42 @@ void do_sysreg(struct cpu_user_regs *regs,
        */
       case HSR_SYSREG_MDSCR_EL1:
           return handle_raz_wi(regs, regidx, hsr.sysreg.read, hsr, 1);
+
+    /*
+     * Xen doesn't expose a real (or emulated) Debug Communications Channel
+     * (DCC) to a domain. Yet the Arm ARM implies this is not an optional
+     * feature. So some domains may start to probe it. For instance, the
+     * HVC_DCC driver in Linux (since f377775dc083 and at least up to v6.7),
+     * will try to write some characters and check if the transmit buffer
+     * has emptied.
+     */
       case HSR_SYSREG_MDCCSR_EL0:
           /*
+         * By setting TX status bit (only if partial emulation is enabled) to
+         * indicate the transmit buffer is full, we would hint the OS that the
+         * DCC is probably not working.
+         *
+         * Bit 29: TX full
+         *
            * Accessible at EL0 only if MDSCR_EL1.TDCC is set to 0. We emulate that
            * register as RAZ/WI above. So RO at both EL0 and EL1.
            */
-        return handle_ro_raz(regs, regidx, hsr.sysreg.read, hsr, 0);
+        return handle_ro_read_val(regs, regidx, hsr.sysreg.read, hsr, 0,
+                                  partial_emulation ? (1U << 29) : 0);
+
+    case HSR_SYSREG_DBGDTR_EL0:
+    /* DBGDTR[TR]X_EL0 share the same encoding */
+    case HSR_SYSREG_DBGDTRTX_EL0:
+        /*
+         * Emulate as RAZ/WI (only if partial emulation is enabled) to prevent
+         * injecting undefined exception.
+         * Accessible at EL0 only if MDSCR_EL1.TDCC is set to 0. We emulate that
+         * register as RAZ/WI.
+         */
+        if ( !partial_emulation )
+            goto fail;
+        return handle_raz_wi(regs, regidx, hsr.sysreg.read, hsr, 0);
+
       HSR_SYSREG_DBG_CASES(DBGBVR):
       HSR_SYSREG_DBG_CASES(DBGBCR):
       HSR_SYSREG_DBG_CASES(DBGWVR):
@@ -394,26 +422,24 @@ void do_sysreg(struct cpu_user_regs *regs,
        * And all other unknown registers.
        */
       default:
-        {
-            const struct hsr_sysreg sysreg = hsr.sysreg;
-
-            gdprintk(XENLOG_ERR,
-                     "%s %d, %d, c%d, c%d, %d %s x%d @ 0x%"PRIregister"\n",
-                     sysreg.read ? "mrs" : "msr",
-                     sysreg.op0, sysreg.op1,
-                     sysreg.crn, sysreg.crm,
-                     sysreg.op2,
-                     sysreg.read ? "=>" : "<=",
-                     sysreg.reg, regs->pc);
-            gdprintk(XENLOG_ERR,
-                     "unhandled 64-bit sysreg access %#"PRIregister"\n",
-                     hsr.bits & HSR_SYSREG_REGS_MASK);
-            inject_undef_exception(regs, hsr);
-            return;
-        }
+        goto fail;
       }
 
Quoted 6 lines
Quoted 5 lines

xen/arch/arm/arm64/vsysreg.c void do_sysreg(struct cpu_user_regs *regs, · line 432

Quoted 5 lines
xen/arch/arm/arm64/vsysreg.c +4 -0
       regs->pc += 4;
+    return;
+
+ fail:
+

No need for this empty line.

Ack

Quoted 7 lines
Quoted 6 lines

xen/arch/arm/arm64/vsysreg.c void do_sysreg(struct cpu_user_regs *regs, · line 437

Quoted 6 lines
xen/arch/arm/arm64/vsysreg.c +5 -0
+    gdprintk(XENLOG_ERR,
+             "%s %d, %d, c%d, c%d, %d %s x%d @ 0x%"PRIregister"\n",
+             sysreg.read ? "mrs" : "msr", sysreg.op0, sysreg.op1, sysreg.crn,
+             sysreg.crm, sysreg.op2, sysreg.read ? "=>" : "<=", sysreg.reg,
+             regs->pc);

The original formatting (i.e. placement of printk args) looked better. I'm not sure why you changed it.

My bad. I changed it to align with 80 chars. I will revert it.

Quoted 7 lines
Quoted 7 lines

xen/arch/arm/arm64/vsysreg.c void do_sysreg(struct cpu_user_regs *regs, · line 442

Quoted 6 lines
xen/arch/arm/arm64/vsysreg.c +5 -0
+    gdprintk(XENLOG_ERR,
+             "unhandled 64-bit sysreg access %#"PRIregister"\n",
+             hsr.bits & HSR_SYSREG_REGS_MASK);
+    inject_undef_exception(regs, hsr);
+    return;

The last 'return' needs to be removed (spotted by Michal)

Ack

- Ayan

Quoted 3 lines
Quoted 1 line

~Michal