Skip to contentSkip to conversation

target/arm: Improve user-mode compatibility with JITs

PATCH In review ~jhogberg 4 participants 7 messages Jun 26, 2023 – Jun 27, 2023

lore
Share & tools
lore

~jhogberg

author
#0 lore raw

v4 cover target/arm: Improve user-mode compatibility with JITs

Changes since v3:

1) Reworded the first commit comment to note that the need to clear cache is implementation-dependent.
2) CTR_EL0.DIC is now cleared in user mode to indicate that IC IVAU must be used.
3) The test case now only uses DC CVAU / IC IVAU when this is required, as indicated by CTR_EL0.{DIC,IDC}. There have been no changes outside of the function mark_code_modified

----

When running in user-mode QEMU currently fails to emulate JITs that use dual-mapped code to get around W^X restrictions, where one mapping is writable and one is executable. As it has no way of knowing that a write to the writable region is reflected in the executable one, it fails to invalidate previously translated code which leads to a crash at best.

(Note that system mode is unaffected as the softmmu is fully aware of what is going on.)

This patch series catches changes to dual-mapped code by honoring the cache management instructions required to make things work on actual hardware.

See https://gitlab.com/qemu-project/qemu/-/issues/1034 for more background information

John Högberg (2):
target/arm: Handle IC IVAU to improve compatibility with JITs tests/tcg/aarch64: Add testcases for IC IVAU and dual-mapped code

 target/arm/cpu.c                  |  13 ++
 target/arm/helper.c               |  47 +++++++-
 tests/tcg/aarch64/Makefile.target |   3 +-
 tests/tcg/aarch64/icivau.c        | 189 ++++++++++++++++++++++++++++++
 4 files changed, 248 insertions(+), 4 deletions(-)
 create mode 100644 tests/tcg/aarch64/icivau.c
Signature · 1 line
2.38.5
author
#1 lore raw

PATCH v4 1/2 target/arm: Handle IC IVAU to improve compatibility with JITs

From: John Högberg <[email protected]>

Unlike architectures with precise self-modifying code semantics (e.g. x86) ARM processors do not maintain coherency for instruction execution and memory, requiring an instruction synchronization barrier on every core that will execute the new code, and on many models also the explicit use of cache management instructions.

While this is required to make JITs work on actual hardware, QEMU has gotten away with not handling this since it does not emulate caches, and unconditionally invalidates code whenever the softmmu or the user-mode page protection logic detects that code has been modified.

Unfortunately the latter does not work in the face of dual-mapped code (a common W^X workaround), where one page is executable and the other is writable: user-mode has no way to connect one with the other as that is only known to the kernel and the emulated application.

This commit works around the issue by telling software that instruction cache invalidation is required by clearing the CPR_EL0.DIC flag (regardless of whether the emulated processor needs it), and then invalidating code in IC IVAU instructions.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1034

---
 target/arm/cpu.c    | 13 +++++++++++++
 target/arm/helper.c | 47 ++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 57 insertions(+), 3 deletions(-)
target/arm/cpu.c +13 -0
index 4d5bb57f07..b82fb46157 100644
@@ -1674,6 +1674,19 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
+    /*
+     * User mode relies on IC IVAU instructions to catch modification of
+     * dual-mapped code.
+     *
+     * Clear CTR_EL0.DIC to ensure that software that honors these flags uses
+     * IC IVAU even if the emulated processor does not normally require it.
+     */
+#ifdef CONFIG_USER_ONLY
+    if (arm_feature(env, ARM_FEATURE_AARCH64)) {
+        cpu->ctr = FIELD_DP64(cpu->ctr, CTR_EL0, DIC, 0);
+    }
+#endif
+
     if (!cpu->has_vfp) {
         uint64_t t;
         uint32_t u;
target/arm/helper.c +44 -3

66 lines not on this page · raw message

Signature · 1 line
2.38.5
Reviewed-by on v4 1/2
#2 lore raw

Re: [PATCH qemu v4 1/2] target/arm: Handle IC IVAU to improve compatibility with JITs

Quoted 44 lines · ~jhogberg

From: John Högberg <[email protected]>

Unlike architectures with precise self-modifying code semantics (e.g. x86) ARM processors do not maintain coherency for instruction execution and memory, requiring an instruction synchronization barrier on every core that will execute the new code, and on many models also the explicit use of cache management instructions.

While this is required to make JITs work on actual hardware, QEMU has gotten away with not handling this since it does not emulate caches, and unconditionally invalidates code whenever the softmmu or the user-mode page protection logic detects that code has been modified.

Unfortunately the latter does not work in the face of dual-mapped code (a common W^X workaround), where one page is executable and the other is writable: user-mode has no way to connect one with the other as that is only known to the kernel and the emulated application.

This commit works around the issue by telling software that instruction cache invalidation is required by clearing the CPR_EL0.DIC flag (regardless of whether the emulated processor needs it), and then invalidating code in IC IVAU instructions.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1034

---
  target/arm/cpu.c    | 13 +++++++++++++
  target/arm/helper.c | 47 ++++++++++++++++++++++++++++++++++++++++++---
  2 files changed, 57 insertions(+), 3 deletions(-)
target/arm/cpu.c +2 -0
index 4d5bb57f07..b82fb46157 100644
@@ -1674,6 +1674,19 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
          return;
      }
 
+    /*
+     * User mode relies on IC IVAU instructions to catch modification of

target/arm/cpu.c static void arm_cpu_realizefn(DeviceState *dev, Error **errp) · line 1688

Quoted 10 lines
target/arm/cpu.c +10 -0
+     * dual-mapped code.
+     *
+     * Clear CTR_EL0.DIC to ensure that software that honors these flags uses
+     * IC IVAU even if the emulated processor does not normally require it.
+     */
+#ifdef CONFIG_USER_ONLY
+    if (arm_feature(env, ARM_FEATURE_AARCH64)) {
+        cpu->ctr = FIELD_DP64(cpu->ctr, CTR_EL0, DIC, 0);
+    }
+#endif

No need to check for aarch64 -- the bit is present at the same location with the same meaning in the v8 aarch32 view: "CTR". (Prior to v8, this bit was part of the Format field, and had value 0 for v6 and v7, so still no need to check before writing this zero.)

Otherwise,

r~

author
#3 lore raw

PATCH v4 2/2 tests/tcg/aarch64: Add testcases for IC IVAU and dual-mapped code

In reply to ~jhogberg

From: John Högberg <[email protected]>

https://gitlab.com/qemu-project/qemu/-/issues/1034

---
 tests/tcg/aarch64/Makefile.target |   3 +-
 tests/tcg/aarch64/icivau.c        | 189 ++++++++++++++++++++++++++++++
 2 files changed, 191 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/aarch64/icivau.c
tests/tcg/aarch64/Makefile.target +2 -1
index 3430fd3cd8..de6566d0d4 100644
@@ -9,9 +9,10 @@ AARCH64_SRC=$(SRC_PATH)/tests/tcg/aarch64
 VPATH 		+= $(AARCH64_SRC)
 
 # Base architecture tests
-AARCH64_TESTS=fcvt pcalign-a64
+AARCH64_TESTS=fcvt pcalign-a64 icivau
 
 fcvt: LDFLAGS+=-lm
+icivau: LDFLAGS+=-lrt
 
 run-fcvt: fcvt
 	$(call run-test,$<,$(QEMU) $<, "$< on $(TARGET_NAME)")
tests/tcg/aarch64/icivau.c new file +189 -0

189 lines not on this page · raw message

Signature · 1 line
2.38.5
Reviewed-by on v4 2/2
#4 lore raw

Re: [PATCH qemu v4 2/2] tests/tcg/aarch64: Add testcases for IC IVAU and dual-mapped code

Quoted 8 lines · ~jhogberg

From: John Högberg <[email protected]>

https://gitlab.com/qemu-project/qemu/-/issues/1034

tests/tcg/aarch64/icivau.c line 116

Quoted 8 lines
tests/tcg/aarch64/icivau.c new file +8 -0
+static int self_modification_test(uint32_t *rw_data, const uint32_t *exec_data)
+{
+    /*
+     * This test is self-modifying in an attempt to cover an edge case where
+     * the IC IVAU instruction invalidates itself.
+     *
+     * Note that the IC IVAU instruction is 16 bytes into the function, in what
+     * will be the same cache line as the modifed instruction on machines with

typo: "modified".

Otherwise

thanks
-- PMM

#5 lore raw
In reply to ~jhogberg
Quoted 10 lines · ~jhogberg

Changes since v3:

1) Reworded the first commit comment to note that the need to clear cache is implementation-dependent.
2) CTR_EL0.DIC is now cleared in user mode to indicate that IC IVAU must be used.
3) The test case now only uses DC CVAU / IC IVAU when this is required, as indicated by CTR_EL0.{DIC,IDC}. There have been no changes outside of the function mark_code_modified

Since there were only a couple of minor nits in this version of the patchset I'm going to apply it to target-arm.next and make those fixups in my tree. Thanks for this bug fix!

thanks
-- PMM

#6 lore raw
Quoted 3 lines

Since there were only a couple of minor nits in this version of the patchset I'm going to apply it to target-arm.next and make those fixups in my tree. Thanks for this bug fix!

Great, thanks for reviewing it :-)

/John