From nobody Sun Nov 24 06:35:08 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=1726693872; cv=none; d=zohomail.com; s=zohoarc; b=YmK2AKMzSgoJm0f+A66ukvjTXlMfeSsYZ3+lCx9f53ED8ywi2EIXw1taydtEQ8y3XBCd9ceRZE2AZmAIa6kJTsEHeIZqLXBzIARqJ/+ARq1tQ1D9vBgUXgsaQYT7Bm9x/BNZSYPxiarsCY5vhTkQeyWonbasTXEjGUc++BkkEGc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1726693872; 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=5KlXLWKKsuXZCMNQAjhT7RVnkOlSJNAfyDCRZsaBjZ4=; b=Oszf0H5eJ8m1xvLKqsJnhdVvUSkGvN87QD0JUeKyG8pvXgtbA1Jd2i2fWfU9imDpWrNOQeqvzJcWPHVDVCkSlhuCD2g37URDnzVoMnbIW7euQrOpJTtf3i3Z5Zn4tH2dGm3f1bJwMmgexuqm6+/mKRoDVppdjNUvVX1EBDBj9BU= 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 172669387291264.18515170696253; Wed, 18 Sep 2024 14:11:12 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sr1tc-0005dD-9U; Wed, 18 Sep 2024 17:07:32 -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 1sr1ta-0005XF-TL for qemu-devel@nongnu.org; Wed, 18 Sep 2024 17:07:30 -0400 Received: from mail-ed1-x52e.google.com ([2a00:1450:4864:20::52e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sr1tX-0001ev-Hv for qemu-devel@nongnu.org; Wed, 18 Sep 2024 17:07:30 -0400 Received: by mail-ed1-x52e.google.com with SMTP id 4fb4d7f45d1cf-5c42bda005eso219314a12.0 for ; Wed, 18 Sep 2024 14:07:27 -0700 (PDT) Received: from draig.lan ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5c42bb492a1sm5347298a12.8.2024.09.18.14.07.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Sep 2024 14:07:22 -0700 (PDT) Received: from draig.lan (localhost [IPv6:::1]) by draig.lan (Postfix) with ESMTP id 847015FC60; Wed, 18 Sep 2024 22:07:13 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1726693646; x=1727298446; 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=5KlXLWKKsuXZCMNQAjhT7RVnkOlSJNAfyDCRZsaBjZ4=; b=VgrEg0TVAgNAe6/eNxcSoPBGTex1+CwM0coff/yu8i+MVDozbd88Sfn6Jncelk++3y SQ/BYCttLHVeRsdwnEKsL5nEtxZKxCacNSU2JHDOwSa9l8Yiyfln5VBxB8FYw/wXPnVU Bip+KgBbXFMqWA/DWkreSM6LqBSpnWT8uxu+iGFieuELhwgZE4L/IpXFF98wEj85jmuP B1c+vk/ujoIAOGbHDCNa/2dN0n5Vdj05u7qa7sqO2r0DdIr1SLAjII1zCF+Ml2+3qZem fca3dQsskQYquf+waGmglqJpbtNEkx8m3nzuZsjrY565y1azC6iH4u8+YryDruNJtrF1 WXyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726693646; x=1727298446; 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=5KlXLWKKsuXZCMNQAjhT7RVnkOlSJNAfyDCRZsaBjZ4=; b=YyOujG07WPTAYy3AQrszBCGP4tsa3ba72ZlCSTFz2PhhiOgX3ahiv4pDwXJD87FJjn F8oq8cU+8X0p4G4/ePfe3txewuR0MK9N1hiogqVbK35kpU6a5tMUFb0Rnw8x2F0tJ+Lu f/TOEePzPtll2ttdfr5fF4ByNa5kS5LLTeZk9oBuLaSzg9P8kcz60QNHzCtpoXt6RMz6 2/WXQLAWKMLn9c5AphH3YCPNWMzY12mOdqCdapxqpRE5hX1DOdsvqOj4RftrTw3rae9P rS7dd8al19apGIfq5etSyGWQzezcZtDzmce5pgOayrdaUhsn1JuYXeURF8DP8obWkR8m OCDw== X-Gm-Message-State: AOJu0YwcMk0twqGlI6rhbGr9fi6qE0sUFLVO73bdXC4yG8lcTqjXW7FA X7XUYpKylIJYTzbJRK+FwG0zihJOJ6qAUhSADpik2bGGvsLfWPT/Ys+wSedVvvw= X-Google-Smtp-Source: AGHT+IE6zIfETBeiJE+ocGnZWg/V0ih0uTqDuK4/siP4pD90Wr58IF1W5EpNyAOKDy/fJp3+XT07xA== X-Received: by 2002:a05:6402:5193:b0:5c2:7002:7cf8 with SMTP id 4fb4d7f45d1cf-5c413e1f43bmr18489470a12.17.1726693645809; Wed, 18 Sep 2024 14:07:25 -0700 (PDT) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , Pierrick Bouvier , Richard Henderson , Alexandre Iooss , Mahmoud Mandour , David Hildenbrand , Ilya Leoshkevich , qemu-s390x@nongnu.org (open list:S390 TCG CPUs) Subject: [PULL 13/18] tests/tcg: add a system test to check memory instrumentation Date: Wed, 18 Sep 2024 22:07:07 +0100 Message-Id: <20240918210712.2336854-14-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20240918210712.2336854-1-alex.bennee@linaro.org> References: <20240918210712.2336854-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::52e; envelope-from=alex.bennee@linaro.org; helo=mail-ed1-x52e.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=unavailable 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: 1726693875034116600 At first I thought I could compile the user-mode test for system mode however we already have a fairly comprehensive test case for system mode in "memory" so lets use that. As tracking every access will quickly build up with "print-access" we add a new mode to track groups of reads and writes to regions. Because the test_data is 16k aligned we can be sure all accesses to it are ones we can count. First we extend the test to report where the test_data region is. Then we expand the pdot() function to track the total number of reads and writes to the region. We have to add some addition pdot() calls to take into account multiple reads/writes in the test loops. Finally we add a python script to integrate the data from the plugin and the output of the test and validate they both agree on the total counts. As some boot codes clear the bss we also add a flag to add a regions worth of writes to the expected total. Signed-off-by: Alex Benn=C3=A9e Reviewed-by: Pierrick Bouvier Message-Id: <20240916085400.1046925-14-alex.bennee@linaro.org> diff --git a/tests/tcg/multiarch/system/memory.c b/tests/tcg/multiarch/syst= em/memory.c index 28080767b2..65a6038a24 100644 --- a/tests/tcg/multiarch/system/memory.c +++ b/tests/tcg/multiarch/system/memory.c @@ -14,26 +14,35 @@ =20 #include #include +#include #include =20 #ifndef CHECK_UNALIGNED # error "Target does not specify CHECK_UNALIGNED" #endif =20 +uint32_t test_read_count; +uint32_t test_write_count; + #define MEM_PAGE_SIZE 4096 /* nominal 4k "pages" */ #define TEST_SIZE (MEM_PAGE_SIZE * 4) /* 4 pages */ =20 #define ARRAY_SIZE(x) ((sizeof(x) / sizeof((x)[0]))) =20 -__attribute__((aligned(MEM_PAGE_SIZE))) +__attribute__((aligned(TEST_SIZE))) static uint8_t test_data[TEST_SIZE]; =20 typedef void (*init_ufn) (int offset); typedef bool (*read_ufn) (int offset); typedef bool (*read_sfn) (int offset, bool nf); =20 -static void pdot(int count) +static void pdot(int count, bool write) { + if (write) { + test_write_count++; + } else { + test_read_count++; + } if (count % 128 =3D=3D 0) { ml_printf("."); } @@ -67,7 +76,7 @@ static void init_test_data_u8(int unused_offset) =20 for (i =3D 0; i < TEST_SIZE; i++) { *ptr++ =3D BYTE_NEXT(count); - pdot(i); + pdot(i, true); } =20 ml_printf("done %d @ %p\n", i, ptr); @@ -93,8 +102,9 @@ static void init_test_data_s8(bool neg_first) neg_first ? "neg first" : "pos first"); for (i =3D 0; i < TEST_SIZE / 2; i++) { *ptr++ =3D get_byte(i, neg_first); + pdot(i, true); *ptr++ =3D get_byte(i, !neg_first); - pdot(i); + pdot(i, true); } ml_printf("done %d @ %p\n", i * 2, ptr); } @@ -116,6 +126,7 @@ static void reset_start_data(int offset) =20 for (i =3D 0; i < offset; i++) { *ptr++ =3D 0; + pdot(i, true); } =20 ml_printf("done %d @ %p\n", i, ptr); @@ -136,7 +147,7 @@ static void init_test_data_u16(int offset) uint16_t low =3D BYTE_NEXT(count), high =3D BYTE_NEXT(count); word =3D BYTE_SHIFT(high, 1) | BYTE_SHIFT(low, 0); *ptr++ =3D word; - pdot(i); + pdot(i, true); } ml_printf("done %d @ %p\n", i, ptr); } @@ -158,7 +169,7 @@ static void init_test_data_u32(int offset) word =3D BYTE_SHIFT(b1, 3) | BYTE_SHIFT(b2, 2) | BYTE_SHIFT(b3, 1)= | BYTE_SHIFT(b4, 0); *ptr++ =3D word; - pdot(i); + pdot(i, true); } ml_printf("done %d @ %p\n", i, ptr); } @@ -184,7 +195,7 @@ static void init_test_data_u64(int offset) BYTE_SHIFT(b4, 4) | BYTE_SHIFT(b5, 3) | BYTE_SHIFT(b6, 2) | BYTE_SHIFT(b7, 1) | BYTE_SHIFT(b8, 0); *ptr++ =3D word; - pdot(i); + pdot(i, true); } ml_printf("done %d @ %p\n", i, ptr); } @@ -207,7 +218,7 @@ static bool read_test_data_u16(int offset) ml_printf("Error %d < %d\n", high, low); return false; } else { - pdot(i); + pdot(i, false); } =20 } @@ -249,7 +260,7 @@ static bool read_test_data_u32(int offset) ml_printf("Error %d, %d, %d, %d", b1, b2, b3, b4); return false; } else { - pdot(i); + pdot(i, false); } } ml_printf("done %d @ %p\n", i, ptr); @@ -304,7 +315,7 @@ static bool read_test_data_u64(int offset) b1, b2, b3, b4, b5, b6, b7, b8); return false; } else { - pdot(i); + pdot(i, false); } } ml_printf("done %d @ %p\n", i, ptr); @@ -376,9 +387,11 @@ static bool read_test_data_s8(int offset, bool neg_fir= st) second =3D *ptr++; =20 if (neg_first && first < 0 && second > 0) { - pdot(i); + pdot(i, false); + pdot(i, false); } else if (!neg_first && first > 0 && second < 0) { - pdot(i); + pdot(i, false); + pdot(i, false); } else { ml_printf("Error %d %c %d\n", first, neg_first ? '<' : '>', se= cond); return false; @@ -409,9 +422,9 @@ static bool read_test_data_s16(int offset, bool neg_fir= st) int32_t data =3D *ptr++; =20 if (neg_first && data < 0) { - pdot(i); + pdot(i, false); } else if (!neg_first && data > 0) { - pdot(i); + pdot(i, false); } else { ml_printf("Error %d %c 0\n", data, neg_first ? '<' : '>'); return false; @@ -442,9 +455,9 @@ static bool read_test_data_s32(int offset, bool neg_fir= st) int64_t data =3D *ptr++; =20 if (neg_first && data < 0) { - pdot(i); + pdot(i, false); } else if (!neg_first && data > 0) { - pdot(i); + pdot(i, false); } else { ml_printf("Error %d %c 0\n", data, neg_first ? '<' : '>'); return false; @@ -498,6 +511,9 @@ int main(void) int i; bool ok =3D true; =20 + ml_printf("Test data start: 0x%"PRIxPTR"\n", &test_data[0]); + ml_printf("Test data end: 0x%"PRIxPTR"\n", &test_data[TEST_SIZE]); + /* Run through the unsigned tests first */ for (i =3D 0; i < ARRAY_SIZE(init_ufns) && ok; i++) { ok =3D do_unsigned_test(init_ufns[i]); @@ -513,6 +529,8 @@ int main(void) ok =3D do_signed_reads(true); } =20 + ml_printf("Test data read: %"PRId32"\n", test_read_count); + ml_printf("Test data write: %"PRId32"\n", test_write_count); ml_printf("Test complete: %s\n", ok ? "PASSED" : "FAILED"); return ok ? 0 : -1; } diff --git a/tests/tcg/plugins/mem.c b/tests/tcg/plugins/mem.c index 086e6f5bdf..121210157f 100644 --- a/tests/tcg/plugins/mem.c +++ b/tests/tcg/plugins/mem.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include =20 @@ -26,13 +27,47 @@ typedef struct { const char *sym; } InsnInfo; =20 +/* + * For the "memory" system test we need to track accesses to + * individual regions. We mirror the data written to the region and + * then check when it is read that it matches up. + * + * We do this as regions rather than pages to save on complications + * with page crossing and the fact the test only cares about the + * test_data region. + */ +static uint64_t region_size =3D 4096 * 4; +static uint64_t region_mask; + +typedef struct { + uint64_t region_address; + uint64_t reads; + uint64_t writes; + uint8_t *data; + /* Did we see every write and read with correct values? */ + bool seen_all; +} RegionInfo; + static struct qemu_plugin_scoreboard *counts; static qemu_plugin_u64 mem_count; static qemu_plugin_u64 io_count; -static bool do_inline, do_callback, do_print_accesses; +static bool do_inline, do_callback, do_print_accesses, do_region_summary; static bool do_haddr; static enum qemu_plugin_mem_rw rw =3D QEMU_PLUGIN_MEM_RW; =20 + +static GMutex lock; +static GHashTable *regions; + +static gint addr_order(gconstpointer a, gconstpointer b) +{ + RegionInfo *na =3D (RegionInfo *) a; + RegionInfo *nb =3D (RegionInfo *) b; + + return na->region_address > nb->region_address ? 1 : -1; +} + + static void plugin_exit(qemu_plugin_id_t id, void *p) { g_autoptr(GString) out =3D g_string_new(""); @@ -46,9 +81,133 @@ static void plugin_exit(qemu_plugin_id_t id, void *p) qemu_plugin_u64_sum(io_count)); } qemu_plugin_outs(out->str); + + + if (do_region_summary) { + GList *counts =3D g_hash_table_get_values(regions); + + counts =3D g_list_sort(counts, addr_order); + + g_string_printf(out, "Region Base, Reads, Writes, Seen all\n"); + + if (counts && g_list_next(counts)) { + for (/* counts */; counts; counts =3D counts->next) { + RegionInfo *ri =3D (RegionInfo *) counts->data; + + g_string_append_printf(out, + "0x%016"PRIx64", " + "%"PRId64", %"PRId64", %s\n", + ri->region_address, + ri->reads, + ri->writes, + ri->seen_all ? "true" : "false"); + } + } + qemu_plugin_outs(out->str); + } + qemu_plugin_scoreboard_free(counts); } =20 +/* + * Update the region tracking info for the access. We split up accesses + * that span regions even though the plugin infrastructure will deliver + * it as a single access. + */ +static void update_region_info(uint64_t region, uint64_t offset, + qemu_plugin_meminfo_t meminfo, + qemu_plugin_mem_value value, + unsigned size) +{ + bool be =3D qemu_plugin_mem_is_big_endian(meminfo); + bool is_store =3D qemu_plugin_mem_is_store(meminfo); + RegionInfo *ri; + bool unseen_data =3D false; + + g_assert(offset + size <=3D region_size); + + g_mutex_lock(&lock); + ri =3D (RegionInfo *) g_hash_table_lookup(regions, GUINT_TO_POINTER(re= gion)); + + if (!ri) { + ri =3D g_new0(RegionInfo, 1); + ri->region_address =3D region; + ri->data =3D g_malloc0(region_size); + ri->seen_all =3D true; + g_hash_table_insert(regions, GUINT_TO_POINTER(region), (gpointer) = ri); + } + + if (is_store) { + ri->writes++; + } else { + ri->reads++; + } + + switch (value.type) { + case QEMU_PLUGIN_MEM_VALUE_U8: + if (is_store) { + ri->data[offset] =3D value.data.u8; + } else if (ri->data[offset] !=3D value.data.u8) { + unseen_data =3D true; + } + break; + case QEMU_PLUGIN_MEM_VALUE_U16: + { + uint16_t *p =3D (uint16_t *) &ri->data[offset]; + uint16_t val =3D be ? htobe16(value.data.u16) : htole16(value.data= .u16); + if (is_store) { + *p =3D val; + } else if (*p !=3D val) { + unseen_data =3D true; + } + break; + } + case QEMU_PLUGIN_MEM_VALUE_U32: + { + uint32_t *p =3D (uint32_t *) &ri->data[offset]; + uint32_t val =3D be ? htobe32(value.data.u32) : htole32(value.data= .u32); + if (is_store) { + *p =3D val; + } else if (*p !=3D val) { + unseen_data =3D true; + } + break; + } + case QEMU_PLUGIN_MEM_VALUE_U64: + { + uint64_t *p =3D (uint64_t *) &ri->data[offset]; + uint64_t val =3D be ? htobe64(value.data.u64) : htole64(value.data= .u64); + if (is_store) { + *p =3D val; + } else if (*p !=3D val) { + unseen_data =3D true; + } + break; + } + case QEMU_PLUGIN_MEM_VALUE_U128: + /* non in test so skip */ + break; + default: + g_assert_not_reached(); + } + + /* + * This is expected for regions initialised by QEMU (.text etc) but we + * expect to see all data read and written to the test_data region + * of the memory test. + */ + if (unseen_data && ri->seen_all) { + g_autoptr(GString) error =3D g_string_new("Warning: "); + g_string_append_printf(error, "0x%016"PRIx64":%"PRId64 + " read an un-instrumented value\n", + region, offset); + qemu_plugin_outs(error->str); + ri->seen_all =3D false; + } + + g_mutex_unlock(&lock); +} + static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo, uint64_t vaddr, void *udata) { @@ -63,6 +222,15 @@ static void vcpu_mem(unsigned int cpu_index, qemu_plugi= n_meminfo_t meminfo, } else { qemu_plugin_u64_add(mem_count, cpu_index, 1); } + + if (do_region_summary) { + uint64_t region =3D vaddr & ~region_mask; + uint64_t offset =3D vaddr & region_mask; + qemu_plugin_mem_value value =3D qemu_plugin_mem_get_value(meminfo); + unsigned size =3D 1 << qemu_plugin_mem_size_shift(meminfo); + + update_region_info(region, offset, meminfo, value, size); + } } =20 static void print_access(unsigned int cpu_index, qemu_plugin_meminfo_t mem= info, @@ -117,7 +285,7 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct q= emu_plugin_tb *tb) QEMU_PLUGIN_INLINE_ADD_U64, mem_count, 1); } - if (do_callback) { + if (do_callback || do_region_summary) { qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem, QEMU_PLUGIN_CB_NO_REGS, rw, NULL); @@ -176,6 +344,12 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin= _id_t id, fprintf(stderr, "boolean argument parsing failed: %s\n", o= pt); return -1; } + } else if (g_strcmp0(tokens[0], "region-summary") =3D=3D 0) { + if (!qemu_plugin_bool_parse(tokens[0], tokens[1], + &do_region_summary)) { + fprintf(stderr, "boolean argument parsing failed: %s\n", o= pt); + return -1; + } } else { fprintf(stderr, "option parsing failed: %s\n", opt); return -1; @@ -196,6 +370,11 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin= _id_t id, qemu_plugin_outs(out->str); } =20 + if (do_region_summary) { + region_mask =3D (region_size - 1); + regions =3D g_hash_table_new(NULL, g_direct_equal); + } + counts =3D qemu_plugin_scoreboard_new(sizeof(CPUCount)); mem_count =3D qemu_plugin_scoreboard_u64_in_struct( counts, CPUCount, mem_count); diff --git a/tests/tcg/alpha/Makefile.softmmu-target b/tests/tcg/alpha/Make= file.softmmu-target index a0eca4d6ea..a944102a3c 100644 --- a/tests/tcg/alpha/Makefile.softmmu-target +++ b/tests/tcg/alpha/Makefile.softmmu-target @@ -28,7 +28,7 @@ LDFLAGS+=3D-static -nostdlib $(CRT_OBJS) $(MINILIB_OBJS) = -lgcc %: %.c $(LINK_SCRIPT) $(CRT_OBJS) $(MINILIB_OBJS) $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $< -o $@ $(LDFLAGS) =20 -memory: CFLAGS+=3D-DCHECK_UNALIGNED=3D0 +memory: CFLAGS+=3D-DCHECK_UNALIGNED=3D0 -mbwx =20 # Running QEMU_OPTS+=3D-serial chardev:output -kernel diff --git a/tests/tcg/multiarch/system/Makefile.softmmu-target b/tests/tcg= /multiarch/system/Makefile.softmmu-target index 32dc0f9830..07be001102 100644 --- a/tests/tcg/multiarch/system/Makefile.softmmu-target +++ b/tests/tcg/multiarch/system/Makefile.softmmu-target @@ -65,3 +65,9 @@ endif =20 MULTIARCH_RUNS +=3D run-gdbstub-memory run-gdbstub-interrupt \ run-gdbstub-untimely-packet run-gdbstub-registers + +# Test plugin memory access instrumentation +run-plugin-memory-with-libmem.so: \ + PLUGIN_ARGS=3D$(COMMA)region-summary=3Dtrue +run-plugin-memory-with-libmem.so: \ + CHECK_PLUGIN_OUTPUT_COMMAND=3D$(MULTIARCH_SYSTEM_SRC)/validate-memory-cou= nts.py $@.out diff --git a/tests/tcg/multiarch/system/validate-memory-counts.py b/tests/t= cg/multiarch/system/validate-memory-counts.py new file mode 100755 index 0000000000..5b8bbf3ef3 --- /dev/null +++ b/tests/tcg/multiarch/system/validate-memory-counts.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python3 +# +# validate-memory-counts.py: check we instrumented memory properly +# +# This program takes two inputs: +# - the mem plugin output +# - the memory binary output +# +# Copyright (C) 2024 Linaro Ltd +# +# SPDX-License-Identifier: GPL-2.0-or-later + +import sys +from argparse import ArgumentParser + +def extract_counts(path): + """ + Load the output from path and extract the lines containing: + + Test data start: 0x40214000 + Test data end: 0x40218001 + Test data read: 2522280 + Test data write: 262111 + + From the stream of data. Extract the values for use in the + validation function. + """ + start_address =3D None + end_address =3D None + read_count =3D 0 + write_count =3D 0 + with open(path, 'r') as f: + for line in f: + if line.startswith("Test data start:"): + start_address =3D int(line.split(':')[1].strip(), 16) + elif line.startswith("Test data end:"): + end_address =3D int(line.split(':')[1].strip(), 16) + elif line.startswith("Test data read:"): + read_count =3D int(line.split(':')[1].strip()) + elif line.startswith("Test data write:"): + write_count =3D int(line.split(':')[1].strip()) + return start_address, end_address, read_count, write_count + + +def parse_plugin_output(path, start, end): + """ + Load the plugin output from path in the form of: + + Region Base, Reads, Writes, Seen all + 0x0000000040004000, 31093, 0, false + 0x0000000040214000, 2522280, 278579, true + 0x0000000040000000, 137398, 0, false + 0x0000000040210000, 54727397, 33721956, false + + And extract the ranges that match test data start and end and + return the results. + """ + total_reads =3D 0 + total_writes =3D 0 + seen_all =3D False + + with open(path, 'r') as f: + next(f) # Skip the header + for line in f: + + if line.startswith("Region Base"): + continue + + parts =3D line.strip().split(', ') + if len(parts) !=3D 4: + continue + + region_base =3D int(parts[0], 16) + reads =3D int(parts[1]) + writes =3D int(parts[2]) + + if start <=3D region_base < end: # Checking if within range + total_reads +=3D reads + total_writes +=3D writes + seen_all =3D parts[3] =3D=3D "true" + + return total_reads, total_writes, seen_all + +def main() -> None: + """ + Process the arguments, injest the program and plugin out and + verify they match up and report if they do not. + """ + parser =3D ArgumentParser(description=3D"Validate memory instrumentati= on") + parser.add_argument('test_output', + help=3D"The output from the test itself") + parser.add_argument('plugin_output', + help=3D"The output from memory plugin") + parser.add_argument('--bss-cleared', + action=3D'store_true', + help=3D'Assume bss was cleared (and adjusts counts= ).') + + args =3D parser.parse_args() + + # Extract counts from memory binary + start, end, exp_reads, exp_writes =3D extract_counts(args.test_output) + + # Some targets clear BSS before running but the test doesn't know + # that so we adjust it by the size of the test region. + if args.bss_cleared: + exp_writes +=3D 16384 + + if start is None or end is None: + print("Failed to test_data boundaries from output.") + sys.exit(1) + + # Parse plugin output + preads, pwrites, seen_all =3D parse_plugin_output(args.plugin_output, + start, end) + + if not seen_all: + print("Fail: didn't instrument all accesses to test_data.") + sys.exit(1) + + # Compare and report + if preads =3D=3D exp_reads and pwrites =3D=3D exp_writes: + sys.exit(0) + else: + print("Fail: The memory reads and writes count does not match.") + print(f"Expected Reads: {exp_reads}, Actual Reads: {preads}") + print(f"Expected Writes: {exp_writes}, Actual Writes: {pwrites}") + sys.exit(1) + +if __name__ =3D=3D "__main__": + main() diff --git a/tests/tcg/s390x/Makefile.softmmu-target b/tests/tcg/s390x/Make= file.softmmu-target index be242ba8f1..3227903348 100644 --- a/tests/tcg/s390x/Makefile.softmmu-target +++ b/tests/tcg/s390x/Makefile.softmmu-target @@ -47,3 +47,8 @@ $(MULTIARCH_TESTS): $(S390X_MULTIARCH_RUNTIME_OBJS) $(MULTIARCH_TESTS): LDFLAGS +=3D $(S390X_MULTIARCH_RUNTIME_OBJS) $(MULTIARCH_TESTS): CFLAGS +=3D $(MINILIB_INC) memory: CFLAGS +=3D -DCHECK_UNALIGNED=3D0 + +# s390x clears the BSS section so we need to account for that +run-plugin-memory-with-libmem.so: \ + CHECK_PLUGIN_OUTPUT_COMMAND=3D$(MULTIARCH_SYSTEM_SRC)/validate-memory-cou= nts.py \ + --bss-cleared $@.out --=20 2.39.5