target/s390x: Fix MVC not always invalidating translation blocks
PATCH 1/2 target/s390x: Fix MVC not always invalidating translation blocks
Node.js crashes in qemu-system-s390x with random SIGSEGVs / SIGILLs.
The v8 JIT used by Node.js can garbage collect and overwrite unused code. Overwriting is performed by WritableJitAllocation::CopyCode(), which ultimately calls memcpy(). For certain sizes, memcpy() uses the MVC instruction.
QEMU implements MVC and other similar instructions using helpers. While TCG store ops invalidate affected translation blocks automatically, helpers must do this manually by calling probe_access_flags(). The MVC helper does this using the access_prepare() -> access_prepare_nf() -> s390_probe_access() -> probe_access_flags() call chain.
At the last step of this chain, the store size is replaced with 0. This causes the probe_access_flags() -> notdirty_write() -> tb_invalidate_phys_range_fast() chain to miss some translation blocks.
When this happens, QEMU executes a mix of old and new code. This quickly leads to either a SIGSEGV or a SIGILL in case the old code ends in the middle of a new instruction.
Fix by passing the true size.
--- target/s390x/tcg/mem_helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/s390x/tcg/mem_helper.c +1 -1
@@ -149,7 +149,7 @@ static inline int s390_probe_access(CPUArchState *env, target_ulong addr, int mmu_idx, bool nonfault, void **phost, uintptr_t ra) { - int flags = probe_access_flags(env, addr, 0, access_type, mmu_idx, + int flags = probe_access_flags(env, addr, size, access_type, mmu_idx, nonfault, phost, ra);if (unlikely (flags &TLB_INVALID_MASK )) {
Signature · 1 line
2.48.1
PATCH 2/2 tests/tcg/s390x: Test modifying code using the MVC instruction
Add a small test to prevent regressions.
--- tests/tcg/s390x/Makefile.softmmu-target | 1 + tests/tcg/s390x/mvc-smc.c | 82 +++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 tests/tcg/s390x/mvc-smc.c
tests/tcg/s390x/Makefile.softmmu-target +1 -0
@@ -42,6 +42,7 @@ $(ASM_TESTS): LDFLAGS += -Wl,-T$(LINK_SCRIPT) -Wl,--build-id=none $(ASM_TESTS): $(LINK_SCRIPT) TESTS += $(ASM_TESTS) +MULTIARCH_TESTS += mvc-smc S390X_MULTIARCH_RUNTIME_OBJS = head64.o console.o $(MINILIB_OBJS) $(MULTIARCH_TESTS): $(S390X_MULTIARCH_RUNTIME_OBJS) $(MULTIARCH_TESTS): LDFLAGS += $(S390X_MULTIARCH_RUNTIME_OBJS)
tests/tcg/s390x/mvc-smc.c new file +82 -0
82 lines not on this page · raw message
Signature · 1 line
2.48.1
Re: [PATCH 2/2] tests/tcg/s390x: Test modifying code using the MVC instruction
Quoted 8 lines · Ilya Leoshkevich
Add a small test to prevent regressions.
Signed-off-by Ilya Leoshkevich <[email protected]>--- tests/tcg/s390x/Makefile.softmmu-target | 1 + tests/tcg/s390x/mvc-smc.c | 82 +++++++++++++++++++++++++ 2 files changed, 83 insertions(+)create mode 100644
tests/tcg/s390x/mvc-smc.c
r~
Quoted 101 lines
tests/tcg/s390x/Makefile.softmmu-target +1
-0@@ -42,6 +42,7 @@ $(ASM_TESTS): LDFLAGS += -Wl,-T$(LINK_SCRIPT) -Wl,--build-id=none $(ASM_TESTS): $(LINK_SCRIPT) TESTS += $(ASM_TESTS) +MULTIARCH_TESTS += mvc-smc S390X_MULTIARCH_RUNTIME_OBJS = head64.o console.o $(MINILIB_OBJS) $(MULTIARCH_TESTS): $(S390X_MULTIARCH_RUNTIME_OBJS) $(MULTIARCH_TESTS): LDFLAGS += $(S390X_MULTIARCH_RUNTIME_OBJS)tests/tcg/s390x/mvc-smc.c new file +82
-0@@ -0,0 +1,82 @@ +/* +* Test modifying code using the MVC instruction. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <minilib.h> + +#define PAGE_SIZE 4096 +#define BR_14_SIZE 2 +#define RWX_OFFSET 2 + +static unsigned char rw[PAGE_SIZE +BR_14_SIZE ]; +static unsigned char rwx[RWX_OFFSET + sizeof(rw)] + __attribute__((aligned(PAGE_SIZE))); + +typedef unsigned long (*function_t )(unsigned long ); + +static int emit_function (unsigned char *p,int n) +{ +int i =0 , val =0 ; + +while (i < n -2 ) { +/* aghi %r2,1 */ + p[i++] =0xa7 ; + p[i++] =0x2b ; + p[i++] =0x00 ; + p[i++] =0x01 ; + val++; + } + +/* br %r14 */ + p[i++] =0x07 ; + p[i++] =0xfe ; + +return val; +} + +static void memcpy_mvc (void *dest,void *src,unsigned long n) +{ +while (n >=256 ) { + asm("mvc 0(256,%[dest]),0(%[src])" + : + : [dest]"a" (dest) + , [src]"a" (src) + :"memory" ); + dest +=256 ; + src +=256 ; + n -=256 ; + } + asm("exrl %[n],0f\n" +"j 1f\n" +"0: mvc 0(1,%[dest]),0(%[src])\n" +"1:" + : + : [dest]"a" (dest) + , [src]"a" (src) + , [n]"a" (n) + :"memory" ); +} + +int main (void ) +{ +int expected, size; + +/* Create a TB. */ + size =sizeof (rwx) -RWX_OFFSET -4 ; + expected =emit_function (rwx +RWX_OFFSET , size); +if (((function_t)(rwx +RWX_OFFSET ))(0 ) != expected) { +return 1 ; + } + +/* Overwrite the TB. */ + size +=4 ; + expected =emit_function (rw, size); +memcpy_mvc (rwx +RWX_OFFSET , rw, size); +if (((function_t)(rwx +RWX_OFFSET ))(0 ) != expected) { +return 2 ; + } + +return 0 ; +}
Quoted 17 lines · Ilya Leoshkevich
Node.js crashes in qemu-system-s390x with random SIGSEGVs / SIGILLs.
The v8 JIT used by Node.js can garbage collect and overwrite unused code. Overwriting is performed by
WritableJitAllocation::CopyCode(), which ultimately callsmemcpy(). For certain sizes,memcpy()uses the MVC instruction.QEMU implements MVC and other similar instructions using helpers. While TCG store ops invalidate affected translation blocks automatically, helpers must do this manually by calling
probe_access_flags(). The MVC helper does this using theaccess_prepare()->access_prepare_nf()->s390_probe_access()->probe_access_flags()call chain.At the last step of this chain, the store size is replaced with 0. This causes the
probe_access_flags()->notdirty_write()->tb_invalidate_phys_range_fast()chain to miss some translation blocks.
Quoted 5 lines
When this happens, QEMU executes a mix of old and new code. This quickly leads to either a SIGSEGV or a SIGILL in case the old code ends in the middle of a new instruction.
Fix by passing the true size.
r~
Quoted 23 lines
Reported-by Berthold Gunreben <[email protected]>Cc Sarah Kriesch <[email protected]>Signed-off-by Ilya Leoshkevich <[email protected]>--- target/s390x/tcg/mem_helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/s390x/tcg/mem_helper.c +1
-1@@ -149,7 +149,7 @@ static inline int s390_probe_access(CPUArchState *env, target_ulong addr, int mmu_idx, bool nonfault, void **phost, uintptr_t ra) { - int flags = probe_access_flags(env, addr, 0, access_type, mmu_idx, + int flags = probe_access_flags(env, addr, size, access_type, mmu_idx, nonfault, phost, ra);if (unlikely (flags &TLB_INVALID_MASK )) {
Quoted 13 lines · Ilya Leoshkevich
Node.js crashes in qemu-system-s390x with random SIGSEGVs / SIGILLs.
The v8 JIT used by Node.js can garbage collect and overwrite unused code. Overwriting is performed by
WritableJitAllocation::CopyCode(), which ultimately callsmemcpy(). For certain sizes,memcpy()uses the MVC instruction.QEMU implements MVC and other similar instructions using helpers. While TCG store ops invalidate affected translation blocks automatically, helpers must do this manually by calling
probe_access_flags(). The MVC helper does this using theaccess_prepare()->access_prepare_nf()->s390_probe_access()->probe_access_flags()call chain.
Quoted 3 lines
At the last step of this chain, the store size is replaced with 0. This causes the
probe_access_flags()->notdirty_write()->tb_invalidate_phys_range_fast()chain to miss some translation blocks.
We added the size parameter in:
commit 1770b2f2d3d6fe8f1e2d61692692264cac44340d
Author: Daniel Henrique Barboza <[email protected]> Date: Thu Feb 23 20:44:24 2023 -0300
accel/tcg: Add 'size' param to probe_access_flags()
probe_access_flags() as it is today uses probe_access_full(), which in turn uses probe_access_internal() with size = 0. probe_access_internal() then uses the size to call the tlb_fill() callback for the given CPU. This size param ('fault_size' as probe_access_internal() calls it) is ignored by most existing .tlb_fill callback implementations, e.g. arm_cpu_tlb_fill(), ppc_cpu_tlb_fill(), x86_cpu_tlb_fill() and mips_cpu_tlb_fill() to name a few.
And added support for more than one byte for the notdirty case in
commit e2faabee78ff127848f59892747d4c07c56de033
Author: Jessica Clarke <[email protected]> Date: Fri Nov 10 21:43:03 2023 -0800
accel/tcg: Forward probe size on to notdirty_write
Without this, we just dirty a single byte, and so if the caller writes more than one byte to the host memory then we won't have invalidated any translation blocks that start after the first byte and overlap those writes.
So I guess it's rather hard to find a "Fixes:" tag, because likely it's been broken forever?
Quoted 6 lines
When this happens, QEMU executes a mix of old and new code. This quickly leads to either a SIGSEGV or a SIGILL in case the old code ends in the middle of a new instruction.
Fix by passing the true size.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 64 lines · David Hildenbrand
Quoted 19 lines · Ilya Leoshkevich
Node.js crashes in qemu-system-s390x with random SIGSEGVs / SIGILLs.
The v8 JIT used by Node.js can garbage collect and overwrite unused code. Overwriting is performed by
WritableJitAllocation::CopyCode(),
which ultimately callsmemcpy(). For certain sizes,memcpy()uses the
MVC instruction.QEMU implements MVC and other similar instructions using helpers. While
TCG store ops invalidate affected translation blocks automatically, helpers must do this manually by callingprobe_access_flags(). The MVC
helper does this using theaccess_prepare()->access_prepare_nf()->s390_probe_access()->probe_access_flags()call chain.Quoted 5 lines
At the last step of this chain, the store size is replaced with 0. This
causes theprobe_access_flags()->notdirty_write()->tb_invalidate_phys_range_fast()chain to miss some translation blocks.We added the size parameter in:
commit
1770b2f2d3d6fe8f1e2d61692692264cac44340d
Author: Daniel Henrique Barboza <[email protected]> Date: Thu Feb 23 20:44:24 2023 -0300accel/tcg: Add 'size' param to
probe_access_flags()
probe_access_flags()as it is today usesprobe_access_full(), which in
turn usesprobe_access_internal()with size = 0.probe_access_internal()
then uses the size to call thetlb_fill()callback for the given CPU.
This size param ('fault_size' asprobe_access_internal()calls it) is
ignored by most existing .tlb_fill callback implementations, e.g.
arm_cpu_tlb_fill(),ppc_cpu_tlb_fill(),x86_cpu_tlb_fill()andmips_cpu_tlb_fill()to name a few.And added support for more than one byte for the notdirty case in
commit
e2faabee78ff127848f59892747d4c07c56de033
Author: Jessica Clarke <[email protected]>
Date: Fri Nov 10 21:43:03 2023 -0800accel/tcg: Forward probe size on to
notdirty_write
Without this, we just dirty a single byte, and so if the caller writes
more than one byte to the host memory then we won't have invalidated any
translation blocks that start after the first byte and overlap those
writes.
Quoted 3 lines
So I guess it's rather hard to find a "Fixes:" tag, because likely it's been
broken forever?
Yes, I thought about this too and gave up.
However, I now wonder if we still need one for non-philosophical, but rather practical backporting reasons? Then
notdirty_write")
(v8.2.0+) should be the right one I think?
Quoted 8 lines
Quoted 5 lines
When this happens, QEMU executes a mix of old and new code. This quickly leads to either a SIGSEGV or a SIGILL in case the old code ends in the middle of a new instruction.
Fix by passing the true size.
Reviewed-by David Hildenbrand <[email protected]>