From nobody Sun Nov 24 21:27:11 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=1720293304; cv=none; d=zohomail.com; s=zohoarc; b=aAKQJMtZZD1q2GCRrVj0VGrvFgXLzu+MjPUSYhnpe4ghzH3Z0HQ8FvpiX0VfDQUqDgDB/jzV8sRYKK5QkDHPpVN/uhrZaNCLaVCM4X5z6mox73Pdat9lxHgaaRi1ZcUgA3XxeOvLRRnNRpRlELk++hpMoZYQPuUu98K7+29BKZo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720293304; h=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=jSYKX7tgmPWgH1eoungM9Ek83i1kIQO2hxPk9JjJ0jw=; b=erguPmvtS0yhNOmxQxfvGygsU+WPHrLARTLSSDz9EKNt1Q6khj9YKyzhAfNkSGDfn0G4zfRBfl1O8SXujn8vXMrmrIT67vC0rxxSpPNcv8T4D8dlZvjcuBvouviHyU8PqUiQZtjdvfO/tWnkhYjr4fp2DgJ12Rxg6UyyZ7Bd/W4= 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 1720293304892564.1699753227607; Sat, 6 Jul 2024 12:15:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sQAr7-0002EJ-9l; Sat, 06 Jul 2024 15:13:57 -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 1sQAr6-0002Du-6D for qemu-devel@nongnu.org; Sat, 06 Jul 2024 15:13:56 -0400 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sQAr3-00045g-Ps for qemu-devel@nongnu.org; Sat, 06 Jul 2024 15:13:55 -0400 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1f65a3abd01so18387685ad.3 for ; Sat, 06 Jul 2024 12:13:53 -0700 (PDT) Received: from linaro.vn.shawcable.net ([2604:3d08:9384:1d00::2193]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fb6153cb3bsm17304305ad.129.2024.07.06.12.13.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Jul 2024 12:13:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1720293231; x=1720898031; 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=jSYKX7tgmPWgH1eoungM9Ek83i1kIQO2hxPk9JjJ0jw=; b=qqwdRoxFtouSAaXWvRQZS+ftVI+mpnYfjpyXEb6Zvxnwr6aX8ISMm/xs3b7maAT1nX Kt7/fMH2t3pPU7P/2VP0E1ydrNInkGwFVZV8ttQUQBUL1Hhrj4WU88VRxsgzDEZjlQxp FhvEQBoI69g8/nR3xfQOl2LQuWlDjUwD5RHcH2iCpwog9b/kvXhkWphxOGpe4f3VFyw2 qhzYn5jEEuEX/1crqlZoeQPbk8az1AV1ztWfdN6bEmqucmZ/Mkgp/Veyr2u0QbXLu67g zhpxTypUE+FRTZNVkwhyrevMKqSf3Jxe4p3Ipc1VNiKwAC5Kw3BW4pjv/8UH9tDux8wK nRgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720293231; x=1720898031; 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=jSYKX7tgmPWgH1eoungM9Ek83i1kIQO2hxPk9JjJ0jw=; b=oxH4S2zu8iybl20twEuKeFo3Fr2DKIUS7BUOvVMbTyrEwVt36pUitlWvMZjROLbn+i 4UZnDoLwIXV/H9vA5G0+/pzTFAdfEFRmN4AJB6pmXhEXp57fzlwwIR32fXFrRD5rfl05 NRZ0C0yv/GqFgKxfebHTLsqdb4738VZVfHMwaPcZCFkugrNl3/wG0m0eEMX91jezpxP8 ygym7Wmnur6Q/b3bLS1yxz8WerAFFO2kRXiwf9MAQTQPy23HvTIg7SFGOPq5mFjMNKlU BiiMW1n0j79UuE1QY79wOmh8C+S3gWXLc1sRm71JsAOV2tr1n3L2QtBXB8jv2PVtOuvV QOZw== X-Gm-Message-State: AOJu0YzX7zyJoW6VrFvqqH42rzt8yk8bIG2mxvWA+TrRb7SYXingTqnX c6noyEMrywqKLsRjKiKeA2x2i7lFYnQ+Akmbd62LhKlPcuYtlxA/ipaxEu+C1xZhRaaclluj5jn r2x8= X-Google-Smtp-Source: AGHT+IFLRSswI9I90e9F7ejRQ5AbBT42voEC2WFxKo2suC+B0G7v/l/zk/eWVLgouY5DebjytqeBhQ== X-Received: by 2002:a17:902:654a:b0:1fb:18a8:1939 with SMTP id d9443c01a7336-1fb33e1965dmr52716065ad.20.1720293231364; Sat, 06 Jul 2024 12:13:51 -0700 (PDT) From: Pierrick Bouvier To: qemu-devel@nongnu.org Cc: Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Richard Henderson , Marcel Apfelbaum , Mahmoud Mandour , Alexandre Iooss , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Eduardo Habkost , Yanan Wang , Pierrick Bouvier , Xingtao Yao Subject: [PATCH v6 7/7] tests/tcg/x86_64: add test for plugin memory access Date: Sat, 6 Jul 2024 12:13:35 -0700 Message-Id: <20240706191335.878142-8-pierrick.bouvier@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240706191335.878142-1-pierrick.bouvier@linaro.org> References: <20240706191335.878142-1-pierrick.bouvier@linaro.org> MIME-Version: 1.0 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=2607:f8b0:4864:20::62c; envelope-from=pierrick.bouvier@linaro.org; helo=mail-pl1-x62c.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: 1720293305467100001 Content-Type: text/plain; charset="utf-8" Add an explicit test to check expected memory values are read/written. For sizes 8, 16, 32, 64 and 128, we generate a load/store operation. For size 8 -> 64, we generate an atomic __sync_val_compare_and_swap too. For 128bits memory access, we rely on SSE2 instructions. By default, atomic accesses are non atomic if a single cpu is running, so we force creation of a second one by creating a new thread first. load/store helpers code path can't be triggered easily in user mode (no softmmu), so we can't test it here. Can be run with: make -C build/tests/tcg/x86_64-linux-user run-plugin-test-plugin-mem-access= -with-libmem.so Tested-by: Xingtao Yao Signed-off-by: Pierrick Bouvier --- tests/tcg/x86_64/test-plugin-mem-access.c | 89 +++++++++++++++++++++ tests/tcg/x86_64/Makefile.target | 7 ++ tests/tcg/x86_64/check-plugin-mem-access.sh | 48 +++++++++++ 3 files changed, 144 insertions(+) create mode 100644 tests/tcg/x86_64/test-plugin-mem-access.c create mode 100755 tests/tcg/x86_64/check-plugin-mem-access.sh diff --git a/tests/tcg/x86_64/test-plugin-mem-access.c b/tests/tcg/x86_64/t= est-plugin-mem-access.c new file mode 100644 index 00000000000..7fdd6a55829 --- /dev/null +++ b/tests/tcg/x86_64/test-plugin-mem-access.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include + +static void *data; + +#define DEFINE_STORE(name, type, value) \ +static void store_##name(void) \ +{ \ + *((type *)data) =3D value; \ +} + +#define DEFINE_ATOMIC_OP(name, type, value) \ +static void atomic_op_##name(void) \ +{ \ + *((type *)data) =3D 0x42; \ + __sync_val_compare_and_swap((type *)data, 0x42, value); \ +} + +#define DEFINE_LOAD(name, type) \ +static void load_##name(void) \ +{ \ + register type var asm("eax") =3D *((type *) data); \ + (void)var; \ +} + +DEFINE_STORE(u8, uint8_t, 0xf1) +DEFINE_ATOMIC_OP(u8, uint8_t, 0xf1) +DEFINE_LOAD(u8, uint8_t) +DEFINE_STORE(u16, uint16_t, 0xf123) +DEFINE_ATOMIC_OP(u16, uint16_t, 0xf123) +DEFINE_LOAD(u16, uint16_t) +DEFINE_STORE(u32, uint32_t, 0xff112233) +DEFINE_ATOMIC_OP(u32, uint32_t, 0xff112233) +DEFINE_LOAD(u32, uint32_t) +DEFINE_STORE(u64, uint64_t, 0xf123456789abcdef) +DEFINE_ATOMIC_OP(u64, uint64_t, 0xf123456789abcdef) +DEFINE_LOAD(u64, uint64_t) + +static void store_u128(void) +{ + _mm_store_si128(data, _mm_set_epi32(0xf1223344, 0x55667788, + 0xf1234567, 0x89abcdef)); +} + +static void load_u128(void) +{ + __m128i var =3D _mm_load_si128(data); + (void)var; +} + +static void *f(void *p) +{ + return NULL; +} + +int main(void) +{ + /* + * We force creation of a second thread to enable cpu flag CF_PARALLEL. + * This will generate atomic operations when needed. + */ + pthread_t thread; + pthread_create(&thread, NULL, &f, NULL); + pthread_join(thread, NULL); + + data =3D malloc(sizeof(__m128i)); + atomic_op_u8(); + store_u8(); + load_u8(); + + atomic_op_u16(); + store_u16(); + load_u16(); + + atomic_op_u32(); + store_u32(); + load_u32(); + + atomic_op_u64(); + store_u64(); + load_u64(); + + store_u128(); + load_u128(); + + free(data); +} diff --git a/tests/tcg/x86_64/Makefile.target b/tests/tcg/x86_64/Makefile.t= arget index eda9bd7396c..3edc29b924d 100644 --- a/tests/tcg/x86_64/Makefile.target +++ b/tests/tcg/x86_64/Makefile.target @@ -16,6 +16,7 @@ X86_64_TESTS +=3D noexec X86_64_TESTS +=3D cmpxchg X86_64_TESTS +=3D adox X86_64_TESTS +=3D test-1648 +PLUGINS_TESTS +=3D test-plugin-mem-access TESTS=3D$(MULTIARCH_TESTS) $(X86_64_TESTS) test-x86_64 else TESTS=3D$(MULTIARCH_TESTS) @@ -26,6 +27,12 @@ adox: CFLAGS=3D-O2 run-test-i386-ssse3: QEMU_OPTS +=3D -cpu max run-plugin-test-i386-ssse3-%: QEMU_OPTS +=3D -cpu max =20 +run-plugin-test-plugin-mem-access-with-libmem.so: \ + PLUGIN_ARGS=3D$(COMMA)print-accesses=3Dtrue +run-plugin-test-plugin-mem-access-with-libmem.so: \ + CHECK_PLUGIN_OUTPUT_COMMAND=3D \ + $(SRC_PATH)/tests/tcg/x86_64/check-plugin-mem-access.sh + 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) diff --git a/tests/tcg/x86_64/check-plugin-mem-access.sh b/tests/tcg/x86_64= /check-plugin-mem-access.sh new file mode 100755 index 00000000000..163f1cfad34 --- /dev/null +++ b/tests/tcg/x86_64/check-plugin-mem-access.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +set -euo pipefail + +die() +{ + echo "$@" 1>&2 + exit 1 +} + +check() +{ + file=3D$1 + pattern=3D$2 + grep "$pattern" "$file" > /dev/null || die "\"$pattern\" not found in = $file" +} + +[ $# -eq 1 ] || die "usage: plugin_out_file" + +plugin_out=3D$1 + +expected() +{ + cat << EOF +,store_u8,.*,8,store,0xf1 +,atomic_op_u8,.*,8,load,0x42 +,atomic_op_u8,.*,8,store,0xf1 +,load_u8,.*,8,load,0xf1 +,store_u16,.*,16,store,0xf123 +,atomic_op_u16,.*,16,load,0x0042 +,atomic_op_u16,.*,16,store,0xf123 +,load_u16,.*,16,load,0xf123 +,store_u32,.*,32,store,0xff112233 +,atomic_op_u32,.*,32,load,0x00000042 +,atomic_op_u32,.*,32,store,0xff112233 +,load_u32,.*,32,load,0xff112233 +,store_u64,.*,64,store,0xf123456789abcdef +,atomic_op_u64,.*,64,load,0x0000000000000042 +,atomic_op_u64,.*,64,store,0xf123456789abcdef +,load_u64,.*,64,load,0xf123456789abcdef +,store_u128,.*,128,store,0xf122334455667788f123456789abcdef +,load_u128,.*,128,load,0xf122334455667788f123456789abcdef +EOF +} + +expected | while read line; do + check "$plugin_out" "$line" +done --=20 2.39.2