Skip to contentSkip to conversation

xen/arm: Add emulation of Debug Data Transfer Registers

PATCH Superseded Ayan Kumar Halder 2 participants 7 messages Jan 5, 2024 – Jan 8, 2024

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 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.

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

 docs/misc/xen-command-line.pandoc    |  7 ++++++
 xen/arch/arm/Kconfig                 |  8 +++++++
 xen/arch/arm/arm64/vsysreg.c         | 25 +++++++++++++++----
 xen/arch/arm/include/asm/arm64/hsr.h |  3 +++
 xen/arch/arm/include/asm/cpregs.h    |  2 ++
 xen/arch/arm/include/asm/regs.h      |  6 +++++
 xen/arch/arm/traps.c                 |  3 +++
 xen/arch/arm/vcpreg.c                | 36 ++++++++++++++++++++--------
 8 files changed, 76 insertions(+), 14 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v3 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. We call them as partial emulation.

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 partially emulated as RAZ/WI and they can be enclosed within CONFIG_PARTIAL_EMULATION.

Further, "partial-emulation" command line option enables us to enable/disable partial emulation at run time. While CONFIG_PARTIAL_EMULATION enables support for partial emulation at compile time (ie 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, customers can use cripts like Imagebuilder to generate uboot-script which will append "partial-emulation=false" to xen command line to turn off 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.

---
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.

 docs/misc/xen-command-line.pandoc | 7 +++++++
 xen/arch/arm/Kconfig              | 8 ++++++++
 xen/arch/arm/include/asm/regs.h   | 6 ++++++
 xen/arch/arm/traps.c              | 3 +++
 4 files changed, 24 insertions(+)
docs/misc/xen-command-line.pandoc +7 -0
index 8e65f8bd18..dd2a76fb19 100644
@@ -1949,6 +1949,13 @@ This option is ignored in **pv-shim** mode.
 
 > Default: `on`
 
+### partial-emulation (arm)
+> `= <boolean>`
+
+> Default: `false`
+
+Flag to enable or disable partial emulation of registers
+
 ### pci
     = List of [ serr=<bool>, perr=<bool> ]
 
xen/arch/arm/Kconfig +8 -0
index 50e9bfae1a..8f25d9cba0 100644
@@ -225,6 +225,14 @@ 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 for registers"
+    default y
+    help
+      This option enabled partial emulation for registers to avoid guests
+      crashing when accessing registers which are not optional but has not been
+      emulated to its complete functionality.
+
 endmenu
 
 menu "ARM errata workaround via the alternative framework"
xen/arch/arm/include/asm/regs.h +6 -0
index f998aedff5..b71fa20f91 100644
@@ -13,6 +13,12 @@
 
 #define psr_mode(psr,m) (((psr) & PSR_MODE_MASK) == (m))
 
+/*
+ * opt_partial_emulation: If true, partial emulation for registers will be
+ * enabled.
+ */
+extern bool opt_partial_emulation;
+
 static inline bool regs_mode_is_32bit(const struct cpu_user_regs *regs)
 {
 #ifdef CONFIG_ARM_32
xen/arch/arm/traps.c +3 -0
index 9c10e8f78c..d5fb9c1035 100644
@@ -42,6 +42,9 @@
 #include <asm/vgic.h>
 #include <asm/vtimer.h>
 
+bool opt_partial_emulation = false;
+boolean_param("partial-emulation", opt_partial_emulation);
+
 /* 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
#2 lore raw

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

Hi Ayan,

Quoted 5 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. We call them as partial emulation.

I read this as if RAZ/WI was the only solution which is not true. I would add e.g.

Quoted 5 lines

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 partially emulated as RAZ/WI and they can be enclosed within CONFIG_PARTIAL_EMULATION.

I think we are missing the purpose of this patch i.e. why we want to enable partial emulation instead of default behavior which is injecting undefined exception.

Quoted 6 lines

Further, "partial-emulation" command line option enables us to enable/disable partial emulation at run time. While CONFIG_PARTIAL_EMULATION enables support for partial emulation at compile time (ie 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, customers

Users (in general) instead of customers?

Quoted 1 line

can use cripts like Imagebuilder to generate uboot-script which will append

s/cripts/scripts

Quoted 6 lines

"partial-emulation=false" to xen command line to turn off 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.

Quoted 21 lines

---
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.

 docs/misc/xen-command-line.pandoc | 7 +++++++
 xen/arch/arm/Kconfig              | 8 ++++++++
 xen/arch/arm/include/asm/regs.h   | 6 ++++++
 xen/arch/arm/traps.c              | 3 +++
 4 files changed, 24 insertions(+)
docs/misc/xen-command-line.pandoc +0 -0
index 8e65f8bd18..dd2a76fb19 100644

docs/misc/xen-command-line.pandoc This option is ignored in **pv-shim** mode. · line 1957

Quoted 9 lines
docs/misc/xen-command-line.pandoc +6 -0
@@ -1949,6 +1949,13 @@ This option is ignored in **pv-shim** mode.
 
 > Default: `on`
 
+### partial-emulation (arm)
+> `= <boolean>`
+
+> Default: `false`
+
+Flag to enable or disable partial emulation of registers

Missing dot at the end of sentence.
Also, I would add a warning that enabling this option might result in unwanted behavior and that it is only effective if CONFIG_PARTIAL_EMULATION is enabled.

Quoted 9 lines
docs/misc/xen-command-line.pandoc +1 -0
+
 ### pci
     = List of [ serr=<bool>, perr=<bool> ]
 
xen/arch/arm/Kconfig +0 -0
index 50e9bfae1a..8f25d9cba0 100644

xen/arch/arm/Kconfig config STATIC_EVTCHN · line 229

Quoted 5 lines
xen/arch/arm/Kconfig +2 -0
@@ -225,6 +225,14 @@ 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 for registers"

Shouldn't we be more specific and write system/coprocessor registers?

xen/arch/arm/Kconfig config STATIC_EVTCHN · line 232

Quoted 3 lines
xen/arch/arm/Kconfig +3 -0
+    default y
+    help
+      This option enabled partial emulation for registers to avoid guests

s/enabled/enables

xen/arch/arm/Kconfig config STATIC_EVTCHN · line 234

Quoted 2 lines
xen/arch/arm/Kconfig +2 -0
+      crashing when accessing registers which are not optional but has not been
+      emulated to its complete functionality.

Ditto about the possible side effect.

Formatting is incorrect. bool, default, help should be indented by tab and help text by tab and 2 spaces.

Quoted 9 lines
xen/arch/arm/Kconfig +1 -0
+
 endmenu
 
 menu "ARM errata workaround via the alternative framework"
xen/arch/arm/include/asm/regs.h +0 -0
index f998aedff5..b71fa20f91 100644

xen/arch/arm/include/asm/regs.h line 19

Quoted 7 lines
xen/arch/arm/include/asm/regs.h +4 -0
@@ -13,6 +13,12 @@
 
 #define psr_mode(psr,m) (((psr) & PSR_MODE_MASK) == (m))
 
+/*
+ * opt_partial_emulation: If true, partial emulation for registers will be
+ * enabled.
+ */

Description would better suit at the definition.

xen/arch/arm/include/asm/regs.h line 21

Quoted 2 lines
xen/arch/arm/include/asm/regs.h +2 -0
+extern bool opt_partial_emulation;
+

Given that parameter definition is in traps.c, I would add declaration in traps.h.

xen/arch/arm/traps.c line 46

Quoted 13 lines
xen/arch/arm/include/asm/regs.h +0 -0
 static inline bool regs_mode_is_32bit(const struct cpu_user_regs *regs)
 {
 #ifdef CONFIG_ARM_32
xen/arch/arm/traps.c +2 -0
index 9c10e8f78c..d5fb9c1035 100644
@@ -42,6 +42,9 @@
 #include <asm/vgic.h>
 #include <asm/vtimer.h>
 
+bool opt_partial_emulation = false;
+boolean_param("partial-emulation", opt_partial_emulation);

Looking at other patches, the partial emulation code will most often be used as follows:
#ifdef CONFIG_PARTIAL_EMULATION
if ( opt_partial_emulation )
...
else
...
#endif

We could instead do:

#ifdef CONFIG_PARTIAL_EMULATION
#define partial_emulation_enabled opt_partial_emulation #else
#define partial_emulation_enabled false
#endif

to reduce the number of checks and ifdefery (still could be used to compile out some code in rare cases).

Quoted 8 lines
xen/arch/arm/traps.c +1 -1
+
 /* 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
--

2.25.1

~Michal

#3 lore raw

PATCH v3 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 (ie DBGDTRTX_EL0 on arm64, DBGDTRTXINT on arm32). As these registers are not emulated, Xen injects an undefined exception to the VM and Linux (running as VM) crashes.

We wish to avoid this crash by adding a partial emulation of DBGDTRTX_EL0. MDCCSR_EL0 is emulated 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(), it returns -ENODEV. In this way, we are preventing the VM from accessing DBGDTRTX_EL0 register.

We also emulate DBGDTR[TR]X_EL0 as RAZ/WI.

---
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.

 xen/arch/arm/arm64/vsysreg.c         | 25 +++++++++++++++++++++----
 xen/arch/arm/include/asm/arm64/hsr.h |  3 +++
 2 files changed, 24 insertions(+), 4 deletions(-)
xen/arch/arm/arm64/vsysreg.c +21 -4
index b5d54c569b..2f70eea2e5 100644
@@ -159,9 +159,6 @@ void do_sysreg(struct cpu_user_regs *regs,
      *
      * Unhandled:
      *    MDCCINT_EL1
-     *    DBGDTR_EL0
-     *    DBGDTRRX_EL0
-     *    DBGDTRTX_EL0
      *    OSDTRRX_EL1
      *    OSDTRTX_EL1
      *    OSECCR_EL1
@@ -172,11 +169,31 @@ void do_sysreg(struct cpu_user_regs *regs,
     case HSR_SYSREG_MDSCR_EL1:
         return handle_raz_wi(regs, regidx, hsr.sysreg.read, hsr, 1);
     case HSR_SYSREG_MDCCSR_EL0:
+    {
         /*
+         * 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. By setting TX status bit 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,
+                                  1U << 29);
+    }
+#ifdef CONFIG_PARTIAL_EMULATION
+    case HSR_SYSREG_DBGDTR_EL0:
+    /* DBGDTR[TR]X_EL0 share the same encoding */
+    case HSR_SYSREG_DBGDTRTX_EL0:
+        if ( opt_partial_emulation )
+            return handle_raz_wi(regs, regidx, hsr.sysreg.read, hsr, 0);
+#endif
     HSR_SYSREG_DBG_CASES(DBGBVR):
     HSR_SYSREG_DBG_CASES(DBGBCR):
     HSR_SYSREG_DBG_CASES(DBGWVR):
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
#4 lore raw

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

Hi Ayan,

Quoted 10 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 (ie DBGDTRTX_EL0 on arm64, DBGDTRTXINT on arm32). As these registers are not emulated, Xen injects an undefined exception to the VM and Linux (running as VM) crashes.

We wish to avoid this crash by adding a partial emulation of DBGDTRTX_EL0. MDCCSR_EL0 is emulated as TXfull.

Quoted 7 lines

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(), it returns -ENODEV. In this way, we are preventing the VM from accessing DBGDTRTX_EL0 register.

AFAIK, we are preventing the VM from making use of HVC DCC as a console. DBGDTRTX_EL0 and MDCCSR_EL0 will be accessed at least once as part of hvc_dcc_check().

Quoted 49 lines

We also emulate DBGDTR[TR]X_EL0 as RAZ/WI.

---
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.

 xen/arch/arm/arm64/vsysreg.c         | 25 +++++++++++++++++++++----
 xen/arch/arm/include/asm/arm64/hsr.h |  3 +++
 2 files changed, 24 insertions(+), 4 deletions(-)
xen/arch/arm/arm64/vsysreg.c +7 -3
index b5d54c569b..2f70eea2e5 100644
@@ -159,9 +159,6 @@ void do_sysreg(struct cpu_user_regs *regs,
      *
      * Unhandled:
      *    MDCCINT_EL1
-     *    DBGDTR_EL0
-     *    DBGDTRRX_EL0
-     *    DBGDTRTX_EL0
      *    OSDTRRX_EL1
      *    OSDTRTX_EL1
      *    OSECCR_EL1
@@ -172,11 +169,31 @@ void do_sysreg(struct cpu_user_regs *regs,
     case HSR_SYSREG_MDSCR_EL1:
         return handle_raz_wi(regs, regidx, hsr.sysreg.read, hsr, 1);
     case HSR_SYSREG_MDCCSR_EL0:
+    {
         /*
+         * 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. By setting TX status bit to indicate the transmit buffer

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

Quoted 10 lines
xen/arch/arm/arm64/vsysreg.c +6 -1
+         * 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,
+                                  1U << 29);

I would assume that if partial emulation is not enabled, we should stick to ro_raz instead of emulating TX set.

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

Quoted 7 lines
xen/arch/arm/arm64/vsysreg.c +7 -0
+    }
+#ifdef CONFIG_PARTIAL_EMULATION
+    case HSR_SYSREG_DBGDTR_EL0:
+    /* DBGDTR[TR]X_EL0 share the same encoding */
+    case HSR_SYSREG_DBGDTRTX_EL0:
+        if ( opt_partial_emulation )
+            return handle_raz_wi(regs, regidx, hsr.sysreg.read, hsr, 0);

This is incorrect. What will happen if CONFIG_PARTIAL_EMULATION=y and opt_partial_emulation=false ? You would fall through to cases below and end up in RAZ/WI. Instead, you should jump to default case, so that we would inject undefined exception.

Quoted 9 lines
xen/arch/arm/arm64/vsysreg.c +1 -0
+#endif
     HSR_SYSREG_DBG_CASES(DBGBVR):
     HSR_SYSREG_DBG_CASES(DBGBCR):
     HSR_SYSREG_DBG_CASES(DBGWVR):
xen/arch/arm/include/asm/arm64/hsr.h +0 -0
index e691d41c17..1495ccddea 100644

xen/arch/arm/include/asm/arm64/hsr.h line 55

Quoted 9 lines
xen/arch/arm/include/asm/arm64/hsr.h +3 -0
@@ -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)

~Michal

#5 lore raw

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

In reply to Ayan Kumar Halder

DBGOSLSR is emulated in the same way as its AArch64 variant (ie OSLSR_EL1). This is to ensure that DBGOSLSR.OSLK is 0, thus MDSCR_EL1.TXfull is treated as UNK/SBZP.
Thus only MDCCSR_EL0 can be emulated (which is DBGDSCRINT on arm32). DBGDSCRINT can be accessed at EL0 as DBGDSCREXT is emulated as RAZ (as DBGOSLSR.OSLK == 0). 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.

---
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.

 xen/arch/arm/include/asm/cpregs.h |  2 ++
 xen/arch/arm/vcpreg.c             | 36 ++++++++++++++++++++++---------
 2 files changed, 28 insertions(+), 10 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 +26 -10
index a2d0500704..474f872b5f 100644
@@ -493,11 +493,12 @@ void do_cp14_32(struct cpu_user_regs *regs, const union hsr hsr)
      * ARMv8 (DDI 0487A.d): D1-1509 Table D1-58
      *
      * Unhandled:
-     *    DBGOSLSR
      *    DBGPRCR
      */
     case HSR_CPREG32(DBGOSLAR):
         return handle_wo_wi(regs, regidx, cp32.read, hsr, 1);
+    case HSR_CPREG32(DBGOSLSR):
+        return handle_ro_read_val(regs, regidx, cp32.read, hsr, 1, 1U << 3);
     case HSR_CPREG32(DBGOSDLR):
         return handle_raz_wi(regs, regidx, cp32.read, hsr, 1);
 
@@ -509,8 +510,6 @@ void do_cp14_32(struct cpu_user_regs *regs, const union hsr hsr)
      *
      * Unhandled:
      *    DBGDCCINT
-     *    DBGDTRRXint
-     *    DBGDTRTXint
      *    DBGWFAR
      *    DBGDTRTXext
      *    DBGDTRRXext,
@@ -549,11 +548,24 @@ void do_cp14_32(struct cpu_user_regs *regs, const union hsr hsr)
     }
 
     case HSR_CPREG32(DBGDSCRINT):
+    {
         /*
-         * Read-only register. Accessible by EL0 if DBGDSCRext.UDCCdis
-         * is set to 0, which we emulated below.
+         * 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. By setting TX status bit to indicate the transmit buffer
+         * is full. This we would hint the OS that the DCC is probably not
+         * working.
+         *
+         * Bit 29: TX full
+         *
+         * Accessible by EL0 if DBGDSCRext.UDCCdis is set to 0, which we emulate
+         * as RAZ/WI in the next case.
          */
-        return handle_ro_raz(regs, regidx, cp32.read, hsr, 1);
+        return handle_ro_read_val(regs, regidx, cp32.read, hsr, 0, 1U << 29);
+    }
 
     case HSR_CPREG32(DBGDSCREXT):
         /*
@@ -562,6 +574,13 @@ void do_cp14_32(struct cpu_user_regs *regs, const union hsr hsr)
          */
         return handle_raz_wi(regs, regidx, cp32.read, hsr, 1);
 
+#ifdef CONFIG_PARTIAL_EMULATION
+    /* DBGDTR[TR]XINT share the same encoding */
+    case HSR_CPREG32(DBGDTRTXINT):
+        if ( opt_partial_emulation )
+            return handle_raz_wi(regs, regidx, cp32.read, hsr, 0);
+#endif
+
     case HSR_CPREG32(DBGVCR):
     case HSR_CPREG32(DBGBVR0):
     case HSR_CPREG32(DBGBCR0):
@@ -659,10 +678,7 @@ void do_cp14_dbg(struct cpu_user_regs *regs, const union hsr hsr)
      * ARMv8 (DDI 0487A.d): D1-1509 Table D1-58
      *
      * Unhandled:
-     *    DBGDTRTXint
-     *    DBGDTRRXint
-     *
-     * And all other unknown registers.
+     * All unknown registers.
      */
     gdprintk(XENLOG_ERR,
              "%s p14, %d, r%d, r%d, cr%d @ 0x%"PRIregister"\n",
Signature · 1 line
2.25.1
#6 lore raw

Re: [XEN v3 3/3] xen/arm: arm32: Add emulation of Debug Data Transfer Registers

Hi Ayan,

Quoted 3 lines · Ayan Kumar Halder

DBGOSLSR is emulated in the same way as its AArch64 variant (ie OSLSR_EL1). This is to ensure that DBGOSLSR.OSLK is 0, thus MDSCR_EL1.TXfull is treated as UNK/SBZP.

No need for this dot and yet another thus (it reads difficult).
You explained the OSLK bit, but you are not emulating this reg as ro_raz. Instead you copied the code from AArch64 (ro_read_val) which also sets OSLM[1] bit. Do we want the same handling given that Linux on arm32 does not make use of it?

Quoted 3 lines

Thus only MDCCSR_EL0 can be emulated (which is DBGDSCRINT on arm32). DBGDSCRINT can be accessed at EL0 as DBGDSCREXT is emulated as RAZ (as DBGOSLSR.OSLK == 0). DBGDSCRINT.TXfull is set to 1.

Even though this patch comes after the one explaining the need of emulating DCC
I would still expect some reasoning here. Someone reading the vcpreg code and checking the commit behind would not know the rationale behind this patch.

Allowing access DBGDSCRINT from EL0 is a fix, so I would make it clear by starting a sentence with "Take the opportunity to fix the minimum EL for DBGDSCRINT ...".

Quoted 108 lines

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.

---
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.

 xen/arch/arm/include/asm/cpregs.h |  2 ++
 xen/arch/arm/vcpreg.c             | 36 ++++++++++++++++++++++---------
 2 files changed, 28 insertions(+), 10 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 +25 -6
index a2d0500704..474f872b5f 100644
@@ -493,11 +493,12 @@ void do_cp14_32(struct cpu_user_regs *regs, const union hsr hsr)
      * ARMv8 (DDI 0487A.d): D1-1509 Table D1-58
      *
      * Unhandled:
-     *    DBGOSLSR
      *    DBGPRCR
      */
     case HSR_CPREG32(DBGOSLAR):
         return handle_wo_wi(regs, regidx, cp32.read, hsr, 1);
+    case HSR_CPREG32(DBGOSLSR):
+        return handle_ro_read_val(regs, regidx, cp32.read, hsr, 1, 1U << 3);
     case HSR_CPREG32(DBGOSDLR):
         return handle_raz_wi(regs, regidx, cp32.read, hsr, 1);
 
@@ -509,8 +510,6 @@ void do_cp14_32(struct cpu_user_regs *regs, const union hsr hsr)
      *
      * Unhandled:
      *    DBGDCCINT
-     *    DBGDTRRXint
-     *    DBGDTRTXint
      *    DBGWFAR
      *    DBGDTRTXext
      *    DBGDTRRXext,
@@ -549,11 +548,24 @@ void do_cp14_32(struct cpu_user_regs *regs, const union hsr hsr)
     }
 
     case HSR_CPREG32(DBGDSCRINT):
+    {
         /*
-         * Read-only register. Accessible by EL0 if DBGDSCRext.UDCCdis
-         * is set to 0, which we emulated below.
+         * 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. By setting TX status bit to indicate the transmit buffer
+         * is full. This we would hint the OS that the DCC is probably not
+         * working.
+         *
+         * Bit 29: TX full
+         *
+         * Accessible by EL0 if DBGDSCRext.UDCCdis is set to 0, which we emulate
+         * as RAZ/WI in the next case.
          */
-        return handle_ro_raz(regs, regidx, cp32.read, hsr, 1);
+        return handle_ro_read_val(regs, regidx, cp32.read, hsr, 0, 1U << 29);
+    }
 
     case HSR_CPREG32(DBGDSCREXT):
         /*
@@ -562,6 +574,13 @@ void do_cp14_32(struct cpu_user_regs *regs, const union hsr hsr)
          */
         return handle_raz_wi(regs, regidx, cp32.read, hsr, 1);
 
+#ifdef CONFIG_PARTIAL_EMULATION
+    /* DBGDTR[TR]XINT share the same encoding */
+    case HSR_CPREG32(DBGDTRTXINT):
+        if ( opt_partial_emulation )
+            return handle_raz_wi(regs, regidx, cp32.read, hsr, 0);
+#endif
+
     case HSR_CPREG32(DBGVCR):
     case HSR_CPREG32(DBGBVR0):
     case HSR_CPREG32(DBGBCR0):
@@ -659,10 +678,7 @@ void do_cp14_dbg(struct cpu_user_regs *regs, const union hsr hsr)
      * ARMv8 (DDI 0487A.d): D1-1509 Table D1-58

xen/arch/arm/vcpreg.c void do_cp14_dbg(struct cpu_user_regs *regs, const union hsr hsr) · line 684

Quoted 10 lines
xen/arch/arm/vcpreg.c +1 -4
      *
      * Unhandled:
-     *    DBGDTRTXint
-     *    DBGDTRRXint
-     *
-     * And all other unknown registers.
+     * All unknown registers.
      */
     gdprintk(XENLOG_ERR,
              "%s p14, %d, r%d, r%d, cr%d @ 0x%"PRIregister"\n",

Same comments apply as for the arm64 patch.

~Michal