diff options
author | Ilya Leoshkevich <iii@linux.ibm.com> | 2023-08-07 13:48:21 +0200 |
---|---|---|
committer | Thomas Huth <thuth@redhat.com> | 2023-08-31 19:10:01 +0200 |
commit | c3513ce5c8736807cb4e5ff529eebadf7a82dbde (patch) | |
tree | fd2f7bde5805be8a36c5a0b4b7c87998a2139ee1 /tests/tcg/s390x/precise-smc-user.c | |
parent | c7f41e4f53c4763bf1e350723a560dc3bf46e04b (diff) |
tests/tcg/s390x: Test precise self-modifying code handling
Add small softmmu and user tests to prevent regressions.
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Message-Id: <20230807114921.438881-2-iii@linux.ibm.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
Diffstat (limited to 'tests/tcg/s390x/precise-smc-user.c')
-rw-r--r-- | tests/tcg/s390x/precise-smc-user.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/tcg/s390x/precise-smc-user.c b/tests/tcg/s390x/precise-smc-user.c new file mode 100644 index 0000000000..33a5270865 --- /dev/null +++ b/tests/tcg/s390x/precise-smc-user.c @@ -0,0 +1,39 @@ +/* + * Test s390x-linux-user precise self-modifying code handling. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#include <assert.h> +#include <sys/mman.h> +#include <stdint.h> +#include <stdlib.h> + +extern __uint128_t __attribute__((__aligned__(1))) smc; +extern __uint128_t __attribute__((__aligned__(1))) patch; + +int main(void) +{ + char *aligned_smc = (char *)((uintptr_t)&smc & ~0xFFFULL); + char *smc_end = (char *)&smc + sizeof(smc); + uint64_t value = 21; + int err; + + err = mprotect(aligned_smc, smc_end - aligned_smc, + PROT_READ | PROT_WRITE | PROT_EXEC); + assert(err == 0); + + asm("jg 0f\n" /* start a new TB */ + "patch: .byte 0,0,0,0,0,0\n" /* replaces padding */ + ".byte 0,0,0,0,0,0\n" /* replaces vstl */ + "agr %[value],%[value]\n" /* replaces sgr */ + "smc: .org . + 6\n" /* pad patched code to 16 bytes */ + "0: vstl %[patch],%[idx],%[smc]\n" /* start writing before TB */ + "sgr %[value],%[value]" /* this becomes `agr %r0,%r0` */ + : [smc] "=R" (smc) + , [value] "+r" (value) + : [patch] "v" (patch) + , [idx] "r" (sizeof(patch) - 1) + : "cc"); + + return value == 42 ? EXIT_SUCCESS : EXIT_FAILURE; +} |