From nobody Sat Nov 23 21:21:18 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1729683609; cv=none; d=zohomail.com; s=zohoarc; b=dNkn2q5CVdYB/85myHoE2g+S4IUAx/jyqhM0iV9vNqmfacTWRqCyoozYva3BrpEPGFGo6ROu3VCrNoRtsIcYDIY0/7J4s0qo3NdICzhT9Jjz26TB2BzSFJhJgEoKjvmkKCrnSwzgIo4XGuXiAE9pM5GjNxDZDyAK5R5OAGu6YPI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1729683609; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=mXxgum5YZeyhem1wJIKkI9XyHJDK2HstmvwMGCvFrKs=; b=cf96ieP2/4EM5hV3JV6P4//0gZjO7/fIbNnG2Gb5xij3MhenjUCzh16819orncX7f8wU0FX7PdnYSIINjI2b7vFk2+ZTRbeZD+0T+MpEL2DgHgGqy6p5HM+aQmSd+W5SapYaMGzPcQTSX5fAx25dIfc6q5xsn6MTVjX9qYTcowc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1729683609075568.4580404545404; Wed, 23 Oct 2024 04:40:09 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t3Zd8-0007Gp-Ky; Wed, 23 Oct 2024 07:34:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t3Zd7-0007Fr-F7 for qemu-devel@nongnu.org; Wed, 23 Oct 2024 07:34:21 -0400 Received: from mail-ed1-x534.google.com ([2a00:1450:4864:20::534]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1t3Zd2-000371-Cw for qemu-devel@nongnu.org; Wed, 23 Oct 2024 07:34:21 -0400 Received: by mail-ed1-x534.google.com with SMTP id 4fb4d7f45d1cf-5c962c3e97dso8032485a12.0 for ; Wed, 23 Oct 2024 04:34:15 -0700 (PDT) Received: from draig.lan ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5cb66a654a2sm4338458a12.38.2024.10.23.04.34.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 04:34:10 -0700 (PDT) Received: from draig.lan (localhost [IPv6:::1]) by draig.lan (Postfix) with ESMTP id 9C8B75F92E; Wed, 23 Oct 2024 12:34:07 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1729683254; x=1730288054; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mXxgum5YZeyhem1wJIKkI9XyHJDK2HstmvwMGCvFrKs=; b=Cs2+K6Gla+tt0AJROazW2TdPiNLDbIEqQT7mTqNbx34ecaieEnTVaA+j+5AK7k/djC quFOBBakbMOmxRGfS3MILF4zIWg+5j6gaJA+sfuRERYNx5Hj2jQcIdDneLIqd+IMW+3m 0YOclhDsXMvTxVx0muvbcxBjHIrDOJvv3Um7rGjAaUc76gL3y7JPAIAMlZoz5PSXs9uJ CrkeuWg31tH0/vtFae2mBSal2wnn3ZcuyWBNsZSop7lGl2xuGNJ1maQr7j/LEpPeLsBw SpdM086Hw0M6SYuGDx3wxjmuJ9k/Ypm34r1U0gUkZbuxZBpyj79QHt43Zi2x7c4RZ0LG K7oQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729683254; x=1730288054; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mXxgum5YZeyhem1wJIKkI9XyHJDK2HstmvwMGCvFrKs=; b=OuvRqygyVXXeeAGcsZsJFJGPuOHh9/X3BMpUfLrZFRwIItJig2CN1bNN0j/zFPeNHg xMeEP5odwDFEUXUTPEoZ3eyHH8p+pLYINTlmfG4a5OWCGiy6Zy3I/SVM/SzbzLnu66O/ ohDV5By0UR/R4OR7pGb3tQrQCYpZU3wp7ALxePoiW3VLQuzcmuTy25nee3ppRG0YZQHD OcmCiDtHAhqODUZv/5Gx7WJ3WNRFzNgK8vuX0hEPVXPcm1+rwRZkiNEkMNUUfeyj98fp fSzUF0KJUXlKgQWEPcd1PUtM7oDx5Ddo4+SJgeYcakaWr1fZCJMN6Pj0MjwHDigY1HvV CEjQ== X-Gm-Message-State: AOJu0YzL/i4sgEe5+/rdgBU32d4s+kcF5IC+wKJjb8mBYcAbTu62+StD gw94+0hEUb5lBPQTKY2AK3U53M/Hdbi9RvzqXEyNZXSVD0YvkLe2B3gt3QV0PvE= X-Google-Smtp-Source: AGHT+IHRTit0g8/bbhEgsxdoStcLwtJwEj0lmJnkwGZ6qbwUYTq1iGytPFaoHqieRWzS0F839R+7jA== X-Received: by 2002:a05:6402:2186:b0:5ca:d533:1c7b with SMTP id 4fb4d7f45d1cf-5cb8af6c62bmr2323197a12.28.1729683254182; Wed, 23 Oct 2024 04:34:14 -0700 (PDT) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: Zhao Liu , Eduardo Habkost , Pierrick Bouvier , Peter Maydell , Yanan Wang , John Snow , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , devel@lists.libvirt.org, Marcelo Tosatti , Riku Voipio , Wainer dos Santos Moschetta , Marcel Apfelbaum , Paolo Bonzini , Mahmoud Mandour , Laurent Vivier , Jiaxun Yang , "Edgar E. Iglesias" , Richard Henderson , Alexandre Iooss , qemu-arm@nongnu.org, =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Cleber Rosa , kvm@vger.kernel.org, Beraldo Leal , Thomas Huth , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Ilya Leoshkevich Subject: [PATCH v3 07/18] tests/tcg/x86_64: Add cross-modifying code test Date: Wed, 23 Oct 2024 12:33:55 +0100 Message-Id: <20241023113406.1284676-8-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241023113406.1284676-1-alex.bennee@linaro.org> References: <20241023113406.1284676-1-alex.bennee@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::534; envelope-from=alex.bennee@linaro.org; helo=mail-ed1-x534.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1729683609809116600 From: Ilya Leoshkevich commit f025692c992c ("accel/tcg: Clear PAGE_WRITE before translation") fixed cross-modifying code handling, but did not add a test. The changed code was further improved recently [1], and I was not sure whether these modifications were safe (spoiler: they were fine). Add a test to make sure there are no regressions. [1] https://lists.gnu.org/archive/html/qemu-devel/2022-09/msg00034.html Signed-off-by: Ilya Leoshkevich Reviewed-by: Pierrick Bouvier Message-Id: <20241022105614.839199-8-alex.bennee@linaro.org> Message-Id: <20241001150617.9977-1-iii@linux.ibm.com> Signed-off-by: Alex Benn=C3=A9e --- tests/tcg/x86_64/cross-modifying-code.c | 80 +++++++++++++++++++++++++ tests/tcg/x86_64/Makefile.target | 4 ++ 2 files changed, 84 insertions(+) create mode 100644 tests/tcg/x86_64/cross-modifying-code.c diff --git a/tests/tcg/x86_64/cross-modifying-code.c b/tests/tcg/x86_64/cro= ss-modifying-code.c new file mode 100644 index 0000000000..2704df6061 --- /dev/null +++ b/tests/tcg/x86_64/cross-modifying-code.c @@ -0,0 +1,80 @@ +/* + * Test patching code, running in one thread, from another thread. + * + * Intel SDM calls this "cross-modifying code" and recommends a special + * sequence, which requires both threads to cooperate. + * + * Linux kernel uses a different sequence that does not require cooperatio= n and + * involves patching the first byte with int3. + * + * Finally, there is user-mode software out there that simply uses atomics= , and + * that seems to be good enough in practice. Test that QEMU has no problems + * with this as well. + */ + +#include +#include +#include +#include + +void add1_or_nop(long *x); +asm(".pushsection .rwx,\"awx\",@progbits\n" + ".globl add1_or_nop\n" + /* addq $0x1,(%rdi) */ + "add1_or_nop: .byte 0x48, 0x83, 0x07, 0x01\n" + "ret\n" + ".popsection\n"); + +#define THREAD_WAIT 0 +#define THREAD_PATCH 1 +#define THREAD_STOP 2 + +static void *thread_func(void *arg) +{ + int val =3D 0x0026748d; /* nop */ + + while (true) { + switch (__atomic_load_n((int *)arg, __ATOMIC_SEQ_CST)) { + case THREAD_WAIT: + break; + case THREAD_PATCH: + val =3D __atomic_exchange_n((int *)&add1_or_nop, val, + __ATOMIC_SEQ_CST); + break; + case THREAD_STOP: + return NULL; + default: + assert(false); + __builtin_unreachable(); + } + } +} + +#define INITIAL 42 +#define COUNT 1000000 + +int main(void) +{ + int command =3D THREAD_WAIT; + pthread_t thread; + long x =3D 0; + int err; + int i; + + err =3D pthread_create(&thread, NULL, &thread_func, &command); + assert(err =3D=3D 0); + + __atomic_store_n(&command, THREAD_PATCH, __ATOMIC_SEQ_CST); + for (i =3D 0; i < COUNT; i++) { + add1_or_nop(&x); + } + __atomic_store_n(&command, THREAD_STOP, __ATOMIC_SEQ_CST); + + err =3D pthread_join(thread, NULL); + assert(err =3D=3D 0); + + assert(x >=3D INITIAL); + assert(x <=3D INITIAL + COUNT); + + return EXIT_SUCCESS; +} diff --git a/tests/tcg/x86_64/Makefile.target b/tests/tcg/x86_64/Makefile.t= arget index 783ab5b21a..d6dff559c7 100644 --- a/tests/tcg/x86_64/Makefile.target +++ b/tests/tcg/x86_64/Makefile.target @@ -17,6 +17,7 @@ X86_64_TESTS +=3D cmpxchg X86_64_TESTS +=3D adox X86_64_TESTS +=3D test-1648 X86_64_TESTS +=3D test-2175 +X86_64_TESTS +=3D cross-modifying-code TESTS=3D$(MULTIARCH_TESTS) $(X86_64_TESTS) test-x86_64 else TESTS=3D$(MULTIARCH_TESTS) @@ -27,6 +28,9 @@ adox: CFLAGS=3D-O2 run-test-i386-ssse3: QEMU_OPTS +=3D -cpu max run-plugin-test-i386-ssse3-%: QEMU_OPTS +=3D -cpu max =20 +cross-modifying-code: CFLAGS+=3D-pthread +cross-modifying-code: LDFLAGS+=3D-pthread + test-x86_64: LDFLAGS+=3D-lm -lc test-x86_64: test-i386.c test-i386.h test-i386-shift.h test-i386-muldiv.h $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) --=20 2.39.5