From: Alexander Spyridakis <a.spyridakis@virtualopensystems.com>
To: mttcg@listserver.greensocs.com
Cc: drjones@redhat.com, kvm@vger.kernel.org,
claudio.fontana@huawei.com, mark.burton@greensocs.com,
qemu-devel@nongnu.org, a.rigo@virtualopensystems.com,
Alexander Spyridakis <a.spyridakis@virtualopensystems.com>,
Jani.Kokkonen@huawei.com, alex.bennee@linaro.org,
fred.konrad@greensocs.com
Subject: [Qemu-devel] [kvm-unit-tests PATCH 1/2] arm/arm64: Add self-modifying code test
Date: Wed, 2 Sep 2015 11:25:25 +0200 [thread overview]
Message-ID: <1441185926-61587-2-git-send-email-a.spyridakis@virtualopensystems.com> (raw)
In-Reply-To: <1441185926-61587-1-git-send-email-a.spyridakis@virtualopensystems.com>
Basic torture test that continuously modifies a single instruction
opcode and checks if all modifications were done and run as expected.
Signed-off-by: Alexander Spyridakis <a.spyridakis@virtualopensystems.com>
---
arm/self-modifying-test.c | 109 +++++++++++++++++++++++++++++++++++++++++++
config/config-arm-common.mak | 2 +
2 files changed, 111 insertions(+)
create mode 100644 arm/self-modifying-test.c
diff --git a/arm/self-modifying-test.c b/arm/self-modifying-test.c
new file mode 100644
index 0000000..cab47a0
--- /dev/null
+++ b/arm/self-modifying-test.c
@@ -0,0 +1,109 @@
+/*
+ * Basic self-modifying code test case
+ *
+ * Copyright (C) 2015 Virtual Open Systems SAS
+ * Author: Alexander Spyridakis <a.spyridakis@virtualopensystems.com>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ */
+
+#include <asm/smp.h>
+#include <asm/mmu.h>
+#include <asm/spinlock.h>
+
+#define LOOP_SIZE 100000
+
+int result;
+static cpumask_t smp_test_complete;
+static struct spinlock lock;
+
+void self_modifying_test(void)
+{
+ extern void *smc;
+ static int toggle;
+ int i, cpu = smp_processor_id(), *ptr = (int *)&smc;
+
+ printf("CPU%d starting test\n", cpu);
+
+ for (i = 0; i < LOOP_SIZE; i++) {
+ /* Don't run concurrently with other CPUs*/
+ spin_lock(&lock);
+
+ /* A simple snippet that increments a memory value which
+ will be modified immediately after. Before running,
+ invalidate the instruction and data cache for that
+ specific virtual address */
+#ifdef __arm__
+ asm("mcr p15, 0, %0, c7, c6, 1\n" /* DCIMVAC */
+ "mcr p15, 0, %0, c7, c5, 1\n" /* ICIMVAU */
+#else
+ asm("dc ivac, %0\n"
+ "ic ivau, %0\n"
+#endif
+ "dsb ish\n"
+ "isb\n"
+ "smc:\n"
+ "add %1, %1, #1\n"
+ "str %1, [%2]\n"
+ :: "r" (ptr), "r" (result), "r" (&result));
+
+ /* Overwrite the previous labelled opcode,
+ toggle between incrementing by one or two */
+ toggle ^= 1;
+ if (toggle)
+#ifdef __arm__
+ *ptr += 1;
+ else
+ *ptr -= 1;
+#else
+ {
+ *ptr &= ~(1 << 10);
+ *ptr |= (1 << 11);
+ } else {
+ *ptr |= (1 << 10);
+ *ptr &= ~(1 << 11);
+ }
+#endif
+
+ spin_unlock(&lock);
+ }
+
+ cpumask_set_cpu(cpu, &smp_test_complete);
+ if (cpu != 0)
+ halt();
+}
+
+int main(int argc, char **argv)
+{
+ int cpu, calc;
+ (void)argc, (void)argv;
+
+ /* Set memory as writeable, on ARMv7 we need to re-enable the MMU */
+#ifdef __arm__
+ mmu_disable();
+ flush_tlb_all();
+ mmu_set_range_ptes(mmu_idmap, PHYS_OFFSET, PHYS_OFFSET, PHYS_END,
+ __pgprot(PTE_WBWA));
+ mmu_enable(mmu_idmap);
+#else
+ mmu_set_range_ptes(mmu_idmap, PHYS_OFFSET, PHYS_OFFSET, PHYS_END,
+ __pgprot(PTE_WBWA));
+ flush_tlb_all();
+#endif
+
+ for_each_present_cpu(cpu) {
+ if (cpu == 0)
+ continue;
+ smp_boot_secondary(cpu, self_modifying_test);
+ }
+
+ self_modifying_test();
+
+ while (!cpumask_full(&smp_test_complete))
+ cpu_relax();
+
+ calc = LOOP_SIZE * nr_cpus + (LOOP_SIZE * nr_cpus / 2);
+ report("Result: %d - Expected: %d\n", result == calc, result, calc);
+
+ return report_summary();
+}
diff --git a/config/config-arm-common.mak b/config/config-arm-common.mak
index 698555d..74a73e4 100644
--- a/config/config-arm-common.mak
+++ b/config/config-arm-common.mak
@@ -10,6 +10,7 @@ ifeq ($(LOADADDR),)
endif
tests-common = \
+ $(TEST_DIR)/self-modifying-test.flat \
$(TEST_DIR)/selftest.flat \
$(TEST_DIR)/spinlock-test.flat
@@ -70,3 +71,4 @@ test_cases: $(generated_files) $(tests-common) $(tests)
$(TEST_DIR)/selftest.elf: $(cstart.o) $(TEST_DIR)/selftest.o
$(TEST_DIR)/spinlock-test.elf: $(cstart.o) $(TEST_DIR)/spinlock-test.o
+$(TEST_DIR)/self-modifying-test.elf: $(cstart.o) $(TEST_DIR)/self-modifying-test.o
--
2.1.4
next prev parent reply other threads:[~2015-09-02 9:25 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-02 9:25 [Qemu-devel] [kvm-unit-tests PATCH 0/2] arm/arm64: Add self-modifying code test case Alexander Spyridakis
2015-09-02 9:25 ` Alexander Spyridakis [this message]
2015-09-02 9:25 ` [Qemu-devel] [kvm-unit-tests PATCH 2/2] arm/arm64 config: Fix arch_clean rule Alexander Spyridakis
2015-09-04 10:48 ` Andrew Jones
2015-09-04 13:48 ` Alexander Spyridakis
2015-09-04 14:05 ` Andrew Jones
2015-09-04 14:18 ` Peter Maydell
2015-09-04 14:53 ` Alexander Spyridakis
2015-09-07 13:35 ` Alexander Spyridakis
2015-09-07 14:37 ` Andrew Jones
2015-09-07 14:59 ` Alexander Spyridakis
2015-09-04 13:53 ` Alexander Spyridakis
2015-09-04 14:08 ` Andrew Jones
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1441185926-61587-2-git-send-email-a.spyridakis@virtualopensystems.com \
--to=a.spyridakis@virtualopensystems.com \
--cc=Jani.Kokkonen@huawei.com \
--cc=a.rigo@virtualopensystems.com \
--cc=alex.bennee@linaro.org \
--cc=claudio.fontana@huawei.com \
--cc=drjones@redhat.com \
--cc=fred.konrad@greensocs.com \
--cc=kvm@vger.kernel.org \
--cc=mark.burton@greensocs.com \
--cc=mttcg@listserver.greensocs.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).