qemu/tests/tcg/s390x/precise-smc-user.c
Ilya Leoshkevich c3513ce5c8 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>
2023-08-31 19:10:01 +02:00

40 lines
1.3 KiB
C

/*
* 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;
}