qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Alexander Spyridakis <a.spyridakis@virtualopensystems.com>
To: qemu-devel@nongnu.org
Cc: mttcg@listserver.greensocs.com, jani.kokkonen@huawei.com,
	tech@virtualopensystems.com, claudio.fontana@huawei.com
Subject: [Qemu-devel] [PATCH 2/2] atomic-test: Add spinlock test case
Date: Thu,  7 May 2015 13:31:42 +0200	[thread overview]
Message-ID: <1430998302-31502-3-git-send-email-a.spyridakis@virtualopensystems.com> (raw)
In-Reply-To: <1430998302-31502-1-git-send-email-a.spyridakis@virtualopensystems.com>

Sample spinlock test case with the option to implement the spinlock
by means of GCC atomic instructions or unsafe memory operations.
Additionally, printf is wrapped around a spinlock to avoid concurrent
access to the serial device.

Suggested-by: Jani Kokkonen <jani.kokkonen@huawei.com>
Suggested-by: Claudio Fontana <claudio.fontana@huawei.com>
Signed-off-by: Alexander Spyridakis <a.spyridakis@virtualopensystems.com>
---
 tests/atomic-test/Makefile  |  3 +++
 tests/atomic-test/helpers.c | 21 +++++++++++++++++++++
 tests/atomic-test/helpers.h | 12 ++++++++++++
 tests/atomic-test/main.c    | 35 ++++++++++++++++++++++++++++++++++-
 tests/atomic-test/printf.c  |  5 +++++
 5 files changed, 75 insertions(+), 1 deletion(-)

diff --git a/tests/atomic-test/Makefile b/tests/atomic-test/Makefile
index 094e01a..d1e992d 100644
--- a/tests/atomic-test/Makefile
+++ b/tests/atomic-test/Makefile
@@ -12,6 +12,9 @@ LD_SCRIPT = link.ld.S
 LIBS      = $(shell $(CC) $(CCFLAGS) -print-libgcc-file-name)
 CPPFLAGS  += -gdwarf-2 -fno-stack-protector -nostdinc -fno-builtin
 
+# Set to ATOMIC to implement spinlock test with real atomic instructions
+TEST = ATOMIC
+
 #
 # Target specific variables
 #
diff --git a/tests/atomic-test/helpers.c b/tests/atomic-test/helpers.c
index 8ac8c2c..fd2d4cf 100644
--- a/tests/atomic-test/helpers.c
+++ b/tests/atomic-test/helpers.c
@@ -82,3 +82,24 @@ void power_off(void)
     /* Shut down system */
     psci_call(PSCI_SYSTEM_OFF, 0, 0, 0);
 }
+
+void atomic_lock(int *lock_var)
+{
+    while (__sync_lock_test_and_set(lock_var, 1));
+}
+
+void atomic_unlock(int *lock_var)
+{
+    __sync_lock_release(lock_var);
+}
+
+void non_atomic_lock(int *lock_var)
+{
+    while (*lock_var != 0);
+    *lock_var = 1;
+}
+
+void non_atomic_unlock(int *lock_var)
+{
+    *lock_var = 0;
+}
diff --git a/tests/atomic-test/helpers.h b/tests/atomic-test/helpers.h
index 66d440e..93036be 100644
--- a/tests/atomic-test/helpers.h
+++ b/tests/atomic-test/helpers.h
@@ -29,6 +29,18 @@
 #define PSCI_CPU_OFF       0x84000002
 #define PSCI_SYSTEM_OFF    0x84000008
 
+#ifdef ATOMIC
+#define LOCK   atomic_lock
+#define UNLOCK atomic_unlock
+#else
+#define LOCK   non_atomic_lock
+#define UNLOCK non_atomic_unlock
+#endif
+
+int global_lock;
+int global_a;
+int global_b;
+
 int get_cpuid(void);
 void power_secondary(void);
 void power_off();
diff --git a/tests/atomic-test/main.c b/tests/atomic-test/main.c
index 72eaf59..3143f7c 100644
--- a/tests/atomic-test/main.c
+++ b/tests/atomic-test/main.c
@@ -15,9 +15,42 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include "helpers.h"
+
+#define LOOP_SIZE 1000000
+
+void test_spinlock()
+{
+    int i, errors = 0;
+    int cpu = get_cpuid();
+
+    for (i = 0; i < LOOP_SIZE; i++) {
+        LOCK(&global_lock);
+
+        if (global_a == (cpu + 1) % 2) {
+            global_a = 1;
+            global_b = 0;
+        } else {
+            global_a = 0;
+            global_b = 1;
+        }
+
+        if (global_a == global_b) {
+            errors++;
+        }
+        UNLOCK(&global_lock);
+    }
+
+    printf("CPU%d: Done - Errors: %d\n", cpu, errors);
+}
+
 void main(void)
 {
-    printf("CPU %d on\n", get_cpuid());
+    if (!get_cpuid()) {
+            printf("Starting test\n");
+    }
+
+    test_spinlock();
     power_off();
 }
 
diff --git a/tests/atomic-test/printf.c b/tests/atomic-test/printf.c
index 7c40d37..78a9e54 100644
--- a/tests/atomic-test/printf.c
+++ b/tests/atomic-test/printf.c
@@ -17,6 +17,8 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include "helpers.h"
+
 #define UARTDR      UART_PHYS
 #define UARTFR      0x18
 #define UARTFR_TXFF (1 << 5)
@@ -28,6 +30,7 @@ typedef __builtin_va_list   va_list;
 
 int *uart_phys = (int *)(UART_PHYS);
 int *uart_busy = (int *)(UART_PHYS + UARTFR);
+int printf_lock;
 
 static void putc(char c)
 {
@@ -72,6 +75,7 @@ void printf(const char *fmt, ...)
     int alt_form;
     unsigned long long val;
 
+    atomic_lock(&printf_lock);
     va_start(ap, fmt);
 
     for (; *fmt; fmt++) {
@@ -149,4 +153,5 @@ void printf(const char *fmt, ...)
     }
 
     va_end(ap);
+    atomic_unlock(&printf_lock);
 }
-- 
2.1.4

  parent reply	other threads:[~2015-05-07 11:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-07 11:31 [Qemu-devel] [PATCH RFC 0/2] virt bare-metal payload infrastructure with atomic test case Alexander Spyridakis
2015-05-07 11:31 ` [Qemu-devel] [PATCH RFC 1/2] atomic-test: Implement ARM and AARCH64 basic bare-metal infrastructure Alexander Spyridakis
2015-05-07 11:31 ` Alexander Spyridakis [this message]
2015-05-11 10:17   ` [Qemu-devel] [PATCH 2/2] atomic-test: Add spinlock test case Andrew Jones
2015-05-11 10:41     ` Alexander Spyridakis
2015-05-07 12:55 ` [Qemu-devel] [PATCH RFC 0/2] virt bare-metal payload infrastructure with atomic " Paolo Bonzini
2015-05-11 10:58   ` Alexander Spyridakis

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=1430998302-31502-3-git-send-email-a.spyridakis@virtualopensystems.com \
    --to=a.spyridakis@virtualopensystems.com \
    --cc=claudio.fontana@huawei.com \
    --cc=jani.kokkonen@huawei.com \
    --cc=mttcg@listserver.greensocs.com \
    --cc=qemu-devel@nongnu.org \
    --cc=tech@virtualopensystems.com \
    /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).