Skip to contentSkip to conversation

virt bare-metal payload infrastructure with atomic test case

RFC PATCH In review Alexander Spyridakis 3 participants 7 messages May 7, 2015 – May 11, 2015

lore
Share & tools
lore
#0 lore raw

cover virt bare-metal payload infrastructure with atomic test case

This series implements a bare-metal test payload for the ARM and AARCH64 virt machine models. With the new direction of TCG to finally get multi-threaded capabilities, simple and easily deployed tests are needed, to reproduce race conditions and intuitively debug QEMU's TCG internals.

The goal of the series is to provide an easy way to create SMP guest test cases with minimal initialization. In its current state the provided features are, SMP functionality through PSCI calls, a simple spinlock test-case, and a minimal printf implementation based on the multiboot test. Parts of this payload have been also tested with normal hosts, as well as KVM guests.

For the example spinlock test, racing errors could not be reproduced in the default single-threaded TCG, even with a non-atomic lock. In KVM the expected behaviour of no errors with regular locks, and some errors with a non-atomic lock was observed. Next steps are to test multi-threaded TCG with this kind of payloads, extend the infrastructure to more complex and sensitive test cases, as well as support different architectures.

This work has been sponsored by Huawei Technologies Dusseldorf GmbH.

Alexander Spyridakis (2):
atomic-test: Implement ARM and AARCH64 basic bare-metal infrastructure
atomic-test: Add spinlock test case

 tests/atomic-test/Makefile  |  66 +++++++++++++++++++
 tests/atomic-test/helpers.c | 105 +++++++++++++++++++++++++++++
 tests/atomic-test/helpers.h |  48 ++++++++++++++
 tests/atomic-test/link.ld.S |  19 ++++++
 tests/atomic-test/main.c    |  65 ++++++++++++++++++
 tests/atomic-test/printf.c  | 157 ++++++++++++++++++++++++++++++++++++++++++++
 tests/atomic-test/start.S   |  54 +++++++++++++++
 7 files changed, 514 insertions(+)
 create mode 100644 tests/atomic-test/Makefile
 create mode 100644 tests/atomic-test/helpers.c
 create mode 100644 tests/atomic-test/helpers.h
 create mode 100644 tests/atomic-test/link.ld.S
 create mode 100644 tests/atomic-test/main.c
 create mode 100644 tests/atomic-test/printf.c
 create mode 100644 tests/atomic-test/start.S
Signature · 1 line
2.1.4
#1 lore raw

RFC PATCH 1/2 atomic-test: Implement ARM and AARCH64 basic bare-metal infrastructure

Minimal payload initialization for the virt machine model. Setup a stack and jump to C code, where CPU0 will boot any discovered secondary cores through PSCI. Immediately after all cores are turned-off and the guest is powered down.

Added printf functionality is based on the multiboot test. In a similar manner this test is standalone and not part of 'make check'.

---
 tests/atomic-test/Makefile  |  63 ++++++++++++++++++
 tests/atomic-test/helpers.c |  84 ++++++++++++++++++++++++
 tests/atomic-test/helpers.h |  36 +++++++++++
 tests/atomic-test/link.ld.S |  19 ++++++
 tests/atomic-test/main.c    |  32 ++++++++++
 tests/atomic-test/printf.c  | 152 ++++++++++++++++++++++++++++++++++++++++++++
 tests/atomic-test/start.S   |  54 ++++++++++++++++
 7 files changed, 440 insertions(+)
 create mode 100644 tests/atomic-test/Makefile
 create mode 100644 tests/atomic-test/helpers.c
 create mode 100644 tests/atomic-test/helpers.h
 create mode 100644 tests/atomic-test/link.ld.S
 create mode 100644 tests/atomic-test/main.c
 create mode 100644 tests/atomic-test/printf.c
 create mode 100644 tests/atomic-test/start.S
tests/atomic-test/Makefile new file +63 -0
new file mode 100644
index 0000000..094e01a
@@ -0,0 +1,63 @@
+#
+# Global variables, sources and tools
+#
+CC = $(CROSS_COMPILE)gcc
+LD = $(CROSS_COMPILE)ld
+
+S_OBJS    = start.o
+C_OBJS    = main.o printf.o helpers.o
+H_DEPS    = helpers.h
+LD_SCRIPT = link.ld.S
+
+LIBS      = $(shell $(CC) $(CCFLAGS) -print-libgcc-file-name)
+CPPFLAGS  += -gdwarf-2 -fno-stack-protector -nostdinc -fno-builtin
+
+#
+# Target specific variables
+#
+clean: export DIRS = build-virt build-virt64
+
+virt:   export CPPFLAGS += -march=armv7-a
+virt64: export CPPFLAGS += -march=armv8-a -mgeneral-regs-only -mstrict-align
+
+virt:   export CROSS_COMPILE ?= arm-none-eabi-
+virt64: export CROSS_COMPILE ?= aarch64-linux-gnu-
+
+virt:   export ARCH = ARCH_ARM
+virt64: export ARCH = ARCH_AARCH64
+
+virt virt64: export UART_PHYS = 0x09000000
+virt virt64: export ENTRY_POINT = 0x40000000
+
+virt virt64: export O_DIR = build-$@/
+virt virt64: export IMAGE = $(O_DIR)[email protected]
+
+#
+# Target build rules
+#
+all: virt virt64
+
+clean:
+	rm -rf $(DIRS)
+
+virt virt64:
+	mkdir -p $(O_DIR)
+	@$(MAKE) $(IMAGE) --no-print-directory
+
+$(IMAGE): $(addprefix $(O_DIR), $(S_OBJS)) \
+          $(addprefix $(O_DIR), $(C_OBJS)) $(H_DEPS) $(O_DIR)link.ld Makefile
+	$(LD) -o $@ $(addprefix $(O_DIR), $(S_OBJS)) \
+          $(addprefix $(O_DIR), $(C_OBJS)) $(LIBS) \
+          --script=$(O_DIR)link.ld -Map $(O_DIR)system.map
+
+$(O_DIR)link.ld: $(LD_SCRIPT)
+	$(CC) -DENTRY_POINT=$(ENTRY_POINT) -D$(ARCH) $(CPPFLAGS) -E -P -C -o $@ $<
+
+$(O_DIR)%.o: %.c $(H_DEPS)
+	$(CC) -DENTRY_POINT=$(ENTRY_POINT) \
+          -DUART_PHYS=$(UART_PHYS) -D$(ARCH) $(CPPFLAGS) -c -o $@ $<
+
+$(O_DIR)%.o: %.S $(H_DEPS)
+	$(CC) -D$(ARCH) $(CPPFLAGS) -c -o $@ $<
+
+.PHONY: all clean virt virt64
tests/atomic-test/helpers.c new file +84 -0

84 lines not on this page · raw message

tests/atomic-test/helpers.h new file +36 -0
new file mode 100644
index 0000000..66d440e
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2015 Virtual Open Systems SAS
+ * Author: Alexander Spyridakis <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef HELPERS_H
+#define HELPERS_H
+
+#ifdef ARCH_ARM
+#define PSCI_CPU_ON        0x84000003
+#define PSCI_AFFINITY_INFO 0x84000004
+#elif ARCH_AARCH64
+#define PSCI_CPU_ON        0xC4000003
+#define PSCI_AFFINITY_INFO 0xC4000004
+#endif
+
+#define PSCI_CPU_OFF       0x84000002
+#define PSCI_SYSTEM_OFF    0x84000008
+
+int get_cpuid(void);
+void power_secondary(void);
+void power_off();
+
+#endif
tests/atomic-test/link.ld.S new file +19 -0
new file mode 100644
index 0000000..e1ab018
@@ -0,0 +1,19 @@
+#ifdef ARCH_ARM
+OUTPUT_FORMAT("elf32-littlearm")
+OUTPUT_ARCH(arm)
+#elif ARCH_AARCH64
+OUTPUT_FORMAT("elf64-littleaarch64")
+OUTPUT_ARCH(aarch64)
+#endif
+
+SECTIONS
+{
+    . = ENTRY_POINT;
+
+    .text   ALIGN(4096) : { *(.text)   }
+    .data   ALIGN(4096) : { *(.data)   }
+    .bss    ALIGN(4096) : { *(.bss)    }
+    .rodata ALIGN(4096) : { *(.rodata) }
+
+    text_end = ALIGN(8);
+}
tests/atomic-test/main.c new file +32 -0
new file mode 100644
index 0000000..72eaf59
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2015 Virtual Open Systems SAS
+ * Author: Alexander Spyridakis <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+void main(void)
+{
+    printf("CPU %d on\n", get_cpuid());
+    power_off();
+}
+
+void init(void)
+{
+    /* Only CPU 0 should be here */
+    if (get_cpuid()) {
+        return;
+    }
+
+    power_secondary();
+}
\ No newline at end of file
tests/atomic-test/printf.c new file +152 -0

152 lines not on this page · raw message

tests/atomic-test/start.S new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
2.1.4
#2 lore raw

PATCH 2/2 atomic-test: Add spinlock test case

In reply to Alexander Spyridakis

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.

---
 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(-)
tests/atomic-test/Makefile +3 -0
index 094e01a..d1e992d 100644
@@ -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
 #
tests/atomic-test/helpers.c +21 -0
index 8ac8c2c..fd2d4cf 100644
@@ -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;
+}
tests/atomic-test/helpers.h +12 -0
index 66d440e..93036be 100644
@@ -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();
tests/atomic-test/main.c +34 -1
index 72eaf59..3143f7c 100644
@@ -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();
 }
 
tests/atomic-test/printf.c +5 -0
index 7c40d37..78a9e54 100644
@@ -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);
 }
Signature · 1 line
2.1.4
#5 lore raw
In reply to Alexander Spyridakis
Quoted 11 lines · Alexander Spyridakis

This series implements a bare-metal test payload for the ARM and AARCH64 virt machine models. With the new direction of TCG to finally get multi-threaded capabilities, simple and easily deployed tests are needed, to reproduce race conditions and intuitively debug QEMU's TCG internals.

The goal of the series is to provide an easy way to create SMP guest test cases with minimal initialization. In its current state the provided features are, SMP functionality through PSCI calls, a simple spinlock test-case, and a minimal printf implementation based on the multiboot test. Parts of this payload have been also tested with normal hosts, as well as KVM guests.

Quoted 6 lines

For the example spinlock test, racing errors could not be reproduced in the default single-threaded TCG, even with a non-atomic lock. In KVM the expected behaviour of no errors with regular locks, and some errors with a non-atomic lock was observed. Next steps are to test multi-threaded TCG with this kind of payloads, extend the infrastructure to more complex and sensitive test cases, as well as support different architectures.

Can we reuse the kvm-unit-tests infrastructure? As you said, the testcases should run just as well with TCG and KVM.

Paolo

#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] atomic-test: Add spinlock test case

In reply to Alexander Spyridakis
Quoted 38 lines · Alexander Spyridakis

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.

---
 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(-)
tests/atomic-test/Makefile +3 -0
index 094e01a..d1e992d 100644
@@ -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
 #
tests/atomic-test/helpers.c +0 -0
index 8ac8c2c..fd2d4cf 100644
@@ -82,3 +82,24 @@ void power_off(void)
     /* Shut down system */
     psci_call(PSCI_SYSTEM_OFF, 0, 0, 0);
 }

tests/atomic-test/helpers.c void power_off(void) · line 94

Quoted 10 lines
tests/atomic-test/helpers.c +10 -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);
+}

Do these builtins actually do anything without enabling the MMU first?

Quoted 71 lines
tests/atomic-test/helpers.c +11 -0
+
+void non_atomic_lock(int *lock_var)
+{
+    while (*lock_var != 0);
+    *lock_var = 1;
+}
+
+void non_atomic_unlock(int *lock_var)
+{
+    *lock_var = 0;
+}
tests/atomic-test/helpers.h +12 -0
index 66d440e..93036be 100644
@@ -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();
tests/atomic-test/main.c +29 -0
index 72eaf59..3143f7c 100644
@@ -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);
+}
+

tests/atomic-test/main.c line 55

Quoted 10 lines
tests/atomic-test/main.c +5 -1
 void main(void)
 {
-    printf("CPU %d on\n", get_cpuid());
+    if (!get_cpuid()) {
+            printf("Starting test\n");
+    }
+
+    test_spinlock();
     power_off();
 }

You could have saved a ton of time by just putting these 50 lines into a new kvm-unit-tests test. If you need the mmu disabled for some reason, then we can add an mmu_disable() to the API.

drew

#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] atomic-test: Add spinlock test case

Hi Andrew,

Quoted 14 lines · Andrew Jones

tests/atomic-test/helpers.c void power_off(void) · line 94

Quoted 10 lines
tests/atomic-test/helpers.c +10 -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);
+}

Do these builtins actually do anything without enabling the MMU first?

Quoted 1 line

From my experience while testing on ARM, without the builtins

atomicity will fail, with our without enabling the MMU.

Quoted 42 lines
Quoted 26 lines
tests/atomic-test/main.c +26 -0
+
+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);
+}
+

tests/atomic-test/main.c line 55

Quoted 10 lines
tests/atomic-test/main.c +5 -1
 void main(void)
 {
-    printf("CPU %d on\n", get_cpuid());
+    if (!get_cpuid()) {
+            printf("Starting test\n");
+    }
+
+    test_spinlock();
     power_off();
 }

You could have saved a ton of time by just putting these 50 lines into a new kvm-unit-tests test. If you need the mmu disabled for some reason, then we can add an mmu_disable() to the API.

Quoted 1 line

drew

The main idea is to start with something very minimal and straightforward for a standalone test similar to the QEMU's multiboot, since the target is to mainly trigger race conditions and test multithreaded TCG.

Thanks.

#6 lore raw
In reply to Paolo Bonzini

Hello Paolo,

Quoted 9 lines · Paolo Bonzini
Quoted 6 lines

For the example spinlock test, racing errors could not be reproduced in the default single-threaded TCG, even with a non-atomic lock. In KVM the expected behaviour of no errors with regular locks, and some errors with a non-atomic lock was observed. Next steps are to test multi-threaded TCG with this kind of payloads, extend the infrastructure to more complex and sensitive test cases, as well as support different architectures.

Can we reuse the kvm-unit-tests infrastructure? As you said, the testcases should run just as well with TCG and KVM.

Eventually yes, this could be an option. Although as mentioned the primary target is TCG and while ideally the tests should run on both, behaviour between the two is not always identical.

I have to note that this work initially was targeting not only guests but also physical platforms for very basic testing, and later I dropped some parts to shape it around the virt machine model.