Skip to contentSkip to conversation

xen/arm: Add emulation of Debug Data Transfer Registers

PATCH In review Ayan Kumar Halder 2 participants 8 messages Mar 7, 2024 – Mar 14, 2024

lore
Share & tools
lore
#0 lore raw

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

v5 :-
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                           | 17 +++++++
 docs/misc/xen-command-line.pandoc    | 11 +++++
 xen/arch/arm/Kconfig                 |  9 ++++
 xen/arch/arm/arm64/vsysreg.c         | 69 +++++++++++++++++++---------
 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                | 62 +++++++++++++++++--------
 9 files changed, 149 insertions(+), 39 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v6 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 (which might result in 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.

v5 :-
1. Dropped R-b
2. Update the commit message and Kconfig message.
3. Update 'SUPPORT.md' message.

 SUPPORT.md                        |  9 +++++++++
 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, 44 insertions(+)
SUPPORT.md +9 -0
index 7eb6875cfa..b49da114ab 100644
@@ -102,6 +102,15 @@ 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
+
+Only the following system registers are security supported:
+
 ### ARM Scalable Vector Extension (SVE/SVE2)
 
 Arm64 domains can use Scalable Vector Extension (SVE/SVE2).
docs/misc/xen-command-line.pandoc +11 -0
index 02896598df..023c5e7225 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 40f834bb71..f8139a773a 100644
@@ -234,6 +234,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 possible
+	  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
#3 lore raw

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

v5 :- 1. Fixed style issues.
2. Removed R-b.
3. Removed HSR_SYSREG_ prefixes from registers.

 SUPPORT.md                           |  5 ++
 xen/arch/arm/arm64/vsysreg.c         | 69 +++++++++++++++++++---------
 xen/arch/arm/include/asm/arm64/hsr.h |  3 ++
 3 files changed, 56 insertions(+), 21 deletions(-)
SUPPORT.md +5 -0
index b49da114ab..afbd820084 100644
@@ -111,6 +111,11 @@ that would normally trigger a fault injection.
 
 Only the following system registers are security supported:
 
+    MDCCSR_EL0
+    DBGDTR_EL0
+    DBGDTRTX_EL0
+    DBGDTRRX_EL0
+
 ### ARM Scalable Vector Extension (SVE/SVE2)
 
 Arm64 domains can use Scalable Vector Extension (SVE/SVE2).
xen/arch/arm/arm64/vsysreg.c +48 -21

101 lines not on this page · raw message

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
#5 lore raw

PATCH v6 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 DBGDTR[TR]XINT (these registers share the same encoding) as RAZ/WI and DBGDSCRINT as TXfull.

Refer ARM DDI 0487J.a ID042523, G8.3.19, DBGDTRTXint:
"If TXfull is set to 1, set DTRTX to UNKNOWN".

As a pre-requisite, DBGOSLSR should be emulated in the same way as its AArch64 variant (i.e. OSLSR_EL1). This is to ensure that DBGOSLSR.OSLK is 0, which allows us to skip the emulation of DBGDSCREXT (TXfull is treated as UNK/SBZP) and focus on DBGDSCRINT. DBGOSLSR.OSLM[1] is set to 1 to mantain consistency with Arm64.

Take the opportunity to fix the minimum EL for DBGDSCRINT, which should be 0.

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

v5 :- 1. Reword the commit message
2. Remove the 'return' at the end of function.

 SUPPORT.md                        |  3 ++
 xen/arch/arm/include/asm/cpregs.h |  2 +
 xen/arch/arm/vcpreg.c             | 62 ++++++++++++++++++++++---------
 3 files changed, 49 insertions(+), 18 deletions(-)
SUPPORT.md +3 -0
index afbd820084..e0ff30cfe9 100644
@@ -115,6 +115,9 @@ Only the following system registers are security supported:
     DBGDTR_EL0
     DBGDTRTX_EL0
     DBGDTRRX_EL0
+    DBGDSCRINT
+    DBGDTRTXINT
+    DBGDTRRXINT
 
 ### ARM Scalable Vector Extension (SVE/SVE2)
 
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 +44 -18

108 lines not on this page · raw message

Signature · 1 line
2.25.1
Acked-by on v6 1/3
#2 lore raw

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

In reply to Ayan Kumar Halder

Hi,

Quoted 6 lines · Ayan Kumar Halder
SUPPORT.md +0 -0
index 7eb6875cfa..b49da114ab 100644
@@ -102,6 +102,15 @@ Extension to the GICv3 interrupt controller to support MSI.
 

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

Quoted 10 lines
SUPPORT.md +8 -0
      Status: Experimental
 
+### ARM/Partial Emulation
+
+Enable partial emulation of registers, otherwise considered unimplemented,
+that would normally trigger a fault injection.
+
+    Status: Supported, with caveats
+
+Only the following system registers are security supported:

This reads slightly odd. I would have have expected a few more registers afterwards. I would have considered to move this sentence to patch #2 or maybe N/A. Anyway, that's not too important (the patch doesn't make sense without the next one). So:

Cheers,

Signature · 1 line
Julien Grall
Acked-by on v6 2/3
#4 lore raw

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

In reply to Ayan Kumar Halder

Hi Ayan,

Quoted 13 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".

Quoted 8 lines

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.

Cheers,

Signature · 1 line
Julien Grall
Acked-by on v6 3/3
#6 lore raw

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

In reply to Ayan Kumar Halder

Hi Ayan,

Quoted 10 lines · 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 DBGDTR[TR]XINT (these registers share the same encoding) as RAZ/WI and DBGDSCRINT as TXfull.

Refer ARM DDI 0487J.a ID042523, G8.3.19, DBGDTRTXint:
"If TXfull is set to 1, set DTRTX to UNKNOWN".

Quoted 7 lines

As a pre-requisite, DBGOSLSR should be emulated in the same way as its AArch64 variant (i.e. OSLSR_EL1). This is to ensure that DBGOSLSR.OSLK is 0, which allows us to skip the emulation of DBGDSCREXT (TXfull is treated as UNK/SBZP) and focus on DBGDSCRINT. DBGOSLSR.OSLM[1] is set to 1 to mantain consistency with Arm64.

Take the opportunity to fix the minimum EL for DBGDSCRINT, which should be 0.

NIT: Strictly speaking Arm32 is using PL (Priviledge) rather than EL (Exception Level) to describe each level :).

Anyway,

Quoted 3 lines

Cheers,

Signature · 1 line
Julien Grall
#7 lore raw
In reply to Ayan Kumar Halder

Hi Ayan,

Quoted 24 lines · Ayan Kumar Halder

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.

v5 :-
1. Change mentioned in individual patches.

Quoted 7 lines

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

I have committed the 3 patches.

Cheers,

Signature · 1 line
Julien Grall