Skip to contentSkip to conversation

Introduce RISC-V

PATCH Superseded Andrew Jones 2 participants 33 messages Jan 24, 2024

Subsystems (7)
KVM Kbuild Library code RISC-V +3 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Introduce RISC-V

This series adds another architecture to kvm-unit-tests (RISC-V, both 32-bit and 64-bit). Much of the code is borrowed from arm/arm64 by mimicking its patterns or by first making the arm code more generic and moving it to the common lib.

This series brings UART, SMP, MMU, and exception handling support. One should be able to start writing CPU validation tests in a mix of C and asm as well as write SBI tests, as is the plan for the SBI verification framework. kvm-unit-tests provides backtraces on asserts and input can be given to the tests through command line arguments, environment variables, and the DT (there's already an ISA string parser for extension detection).

This series only targets QEMU TCG and KVM, but OpenSBI may be replaced with other SBI implementations, such as RustSBI. It's a goal to target bare-metal as soon as possible, so EFI support is already in progress and will be posted soon. More follow on series will come as well, bringing interrupt controller support for timer and PMU testing, support to run tests in usermode, and whatever else people need for their tests.

Thanks,
drew

Andrew Jones (24):
configure: Add ARCH_LIBDIR
riscv: Initial port, hello world
arm/arm64: Move cpumask.h to common lib arm/arm64: Share cpu online, present and idle masks
riscv: Add DT parsing
riscv: Add initial SBI support
riscv: Add run script and unittests.cfg
riscv: Add riscv32 support
riscv: Add exception handling
riscv: Add backtrace support
arm/arm64: Generalize wfe/sev names in smp.c arm/arm64: Remove spinlocks from on_cpu_async arm/arm64: Share on_cpus
riscv: Compile with march
riscv: Add SMP support
arm/arm64: Share memregions
riscv: Populate memregions and switch to page allocator
riscv: Add MMU support
riscv: Enable the MMU in secondaries
riscv: Enable vmalloc
lib: Add strcasecmp and strncasecmp
riscv: Add isa string parsing
gitlab-ci: Add riscv64 tests
MAINTAINERS: Add riscv

 .gitlab-ci.yml               |  16 +++
 MAINTAINERS                  |   8 ++
 Makefile                     |   2 +-
 arm/Makefile.common          |   2 +
 arm/selftest.c               |   3 +-
 configure                    |  16 +++
 lib/arm/asm/gic-v2.h         |   2 +-
 lib/arm/asm/gic-v3.h         |   2 +-
 lib/arm/asm/gic.h            |   2 +-
 lib/arm/asm/setup.h          |  14 --
 lib/arm/asm/smp.h            |  45 +-----
 lib/arm/mmu.c                |   3 +-
 lib/arm/setup.c              |  93 +++----------
 lib/arm/smp.c                | 135 +-----------------
 lib/arm64/asm/cpumask.h      |   1 -
 lib/{arm/asm => }/cpumask.h  |  42 +++++-
 lib/ctype.h                  |  10 ++
 lib/elf.h                    |  11 ++
 lib/ldiv32.c                 |  16 +++
 lib/linux/const.h            |   2 +
 lib/memregions.c             |  82 +++++++++++
 lib/memregions.h             |  29 ++++
 lib/on-cpus.c                | 154 +++++++++++++++++++++
 lib/on-cpus.h                |  14 ++
 lib/riscv/.gitignore         |   1 +
 lib/riscv/asm-offsets.c      |  62 +++++++++
 lib/riscv/asm/asm-offsets.h  |   1 +
 lib/riscv/asm/barrier.h      |  20 +++
 lib/riscv/asm/bitops.h       |  21 +++
 lib/riscv/asm/bug.h          |  20 +++
 lib/riscv/asm/csr.h          |  84 ++++++++++++
 lib/riscv/asm/io.h           |  87 ++++++++++++
 lib/riscv/asm/isa.h          |  18 +++
 lib/riscv/asm/memory_areas.h |   1 +
 lib/riscv/asm/mmu.h          |  26 ++++
 lib/riscv/asm/page.h         |  18 +++
 lib/riscv/asm/pgtable.h      |  42 ++++++
 lib/riscv/asm/processor.h    |  27 ++++
 lib/riscv/asm/ptrace.h       |  46 +++++++
 lib/riscv/asm/sbi.h          |  54 ++++++++
 lib/riscv/asm/setup.h        |  15 ++
 lib/riscv/asm/smp.h          |  29 ++++
 lib/riscv/asm/spinlock.h     |   7 +
 lib/riscv/asm/stack.h        |  12 ++
 lib/riscv/bitops.c           |  47 +++++++
 lib/riscv/io.c               |  97 +++++++++++++
 lib/riscv/mmu.c              | 195 ++++++++++++++++++++++++++
 lib/riscv/processor.c        | 146 ++++++++++++++++++++
 lib/riscv/sbi.c              |  40 ++++++
 lib/riscv/setup.c            | 188 +++++++++++++++++++++++++
 lib/riscv/smp.c              |  70 ++++++++++
 lib/riscv/stack.c            |  32 +++++
 lib/string.c                 |  14 ++
 lib/string.h                 |   2 +
 riscv/Makefile               | 105 ++++++++++++++
 riscv/cstart.S               | 258 +++++++++++++++++++++++++++++++++++
 riscv/flat.lds               |  75 ++++++++++
 riscv/run                    |  38 ++++++
 riscv/sbi.c                  |  41 ++++++
 riscv/selftest.c             | 100 ++++++++++++++
 riscv/sieve.c                |   1 +
 riscv/unittests.cfg          |  37 +++++
 62 files changed, 2514 insertions(+), 267 deletions(-)
 delete mode 100644 lib/arm64/asm/cpumask.h
 rename lib/{arm/asm => }/cpumask.h (72%)
 create mode 100644 lib/memregions.c
 create mode 100644 lib/memregions.h
 create mode 100644 lib/on-cpus.c
 create mode 100644 lib/on-cpus.h
 create mode 100644 lib/riscv/.gitignore
 create mode 100644 lib/riscv/asm-offsets.c
 create mode 100644 lib/riscv/asm/asm-offsets.h
 create mode 100644 lib/riscv/asm/barrier.h
 create mode 100644 lib/riscv/asm/bitops.h
 create mode 100644 lib/riscv/asm/bug.h
 create mode 100644 lib/riscv/asm/csr.h
 create mode 100644 lib/riscv/asm/io.h
 create mode 100644 lib/riscv/asm/isa.h
 create mode 100644 lib/riscv/asm/memory_areas.h
 create mode 100644 lib/riscv/asm/mmu.h
 create mode 100644 lib/riscv/asm/page.h
 create mode 100644 lib/riscv/asm/pgtable.h
 create mode 100644 lib/riscv/asm/processor.h
 create mode 100644 lib/riscv/asm/ptrace.h
 create mode 100644 lib/riscv/asm/sbi.h
 create mode 100644 lib/riscv/asm/setup.h
 create mode 100644 lib/riscv/asm/smp.h
 create mode 100644 lib/riscv/asm/spinlock.h
 create mode 100644 lib/riscv/asm/stack.h
 create mode 100644 lib/riscv/bitops.c
 create mode 100644 lib/riscv/io.c
 create mode 100644 lib/riscv/mmu.c
 create mode 100644 lib/riscv/processor.c
 create mode 100644 lib/riscv/sbi.c
 create mode 100644 lib/riscv/setup.c
 create mode 100644 lib/riscv/smp.c
 create mode 100644 lib/riscv/stack.c
 create mode 100644 riscv/Makefile
 create mode 100644 riscv/cstart.S
 create mode 100644 riscv/flat.lds
 create mode 100755 riscv/run
 create mode 100644 riscv/sbi.c
 create mode 100644 riscv/selftest.c
 create mode 120000 riscv/sieve.c
 create mode 100644 riscv/unittests.cfg
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/24 configure: Add ARCH_LIBDIR

Prepare for an architecture which will share the same lib/$ARCH directory, but be configured with different arch names for different bit widths, i.e. riscv32 -> lib/riscv and riscv64 -> lib/riscv.

---
 Makefile  | 2 +-
 configure | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
Makefile +1 -1
index 602910dda11b..4f35fffc685b 100644
@@ -10,7 +10,7 @@ include config.mak
 VPATH = $(SRCDIR)
 
 libdirs-get = $(shell [ -d "lib/$(1)" ] && echo "lib/$(1) lib/$(1)/asm")
-ARCH_LIBDIRS := $(call libdirs-get,$(ARCH)) $(call libdirs-get,$(TEST_DIR))
+ARCH_LIBDIRS := $(call libdirs-get,$(ARCH_LIBDIR)) $(call libdirs-get,$(TEST_DIR))
 OBJDIRS := $(ARCH_LIBDIRS)
 
 DESTDIR := $(PREFIX)/share/kvm-unit-tests/
configure +2 -0
index 6ee9b27a6af2..ada6512702a1 100755
@@ -198,6 +198,7 @@ fi
 arch_name=$arch
 [ "$arch" = "aarch64" ] && arch="arm64"
 [ "$arch_name" = "arm64" ] && arch_name="aarch64"
+arch_libdir=$arch
 
 if [ -z "$target" ]; then
     target="qemu"
@@ -391,6 +392,7 @@ PREFIX=$prefix
 HOST=$host
 ARCH=$arch
 ARCH_NAME=$arch_name
+ARCH_LIBDIR=$arch_libdir
 PROCESSOR=$processor
 CC=$cross_prefix$cc
 CFLAGS=$cflags
Signature · 1 line
2.43.0
Reviewed-by on v1 1/24
#2 lore raw

Re: [kvm-unit-tests PATCH 01/24] configure: Add ARCH_LIBDIR

Quoted 9 lines · Andrew Jones

Prepare for an architecture which will share the same lib/$ARCH directory, but be configured with different arch names for different bit widths, i.e. riscv32 -> lib/riscv and riscv64 -> lib/riscv.

---
  Makefile  | 2 +-
  configure | 2 ++
  2 files changed, 3 insertions(+), 1 deletion(-)
#3 lore raw

PATCH 2/24 riscv: Initial port, hello world

In reply to Andrew Jones

Add the minimal amount of code possible in order to launch a first test, which just prints "Hello, world" using the expected UART address of the QEMU virt machine. Add files, stubs, and some support, such as barriers and MMIO read/write along the way in order to satisfy the compiler. Basically everything is either copied from the arm64 port of kvm-unit-tests, or at least inspired by it, and, in that case, the RISC-V Linux kernel code was copied.

Run with
qemu-system-riscv64 -nographic -M virt -kernel riscv/selftest.flat

and then go to the monitor (ctrl-a c) and use 'q' to quit, since the unit test will just hang after printing hello world and the exit code.

---
 configure                   | 14 ++++++
 lib/riscv/.gitignore        |  1 +
 lib/riscv/asm-offsets.c     |  6 +++
 lib/riscv/asm/asm-offsets.h |  1 +
 lib/riscv/asm/barrier.h     | 13 ++++++
 lib/riscv/asm/csr.h         |  7 +++
 lib/riscv/asm/io.h          | 78 +++++++++++++++++++++++++++++++
 lib/riscv/asm/page.h        |  7 +++
 lib/riscv/asm/setup.h       |  7 +++
 lib/riscv/asm/spinlock.h    |  7 +++
 lib/riscv/asm/stack.h       |  9 ++++
 lib/riscv/io.c              | 44 ++++++++++++++++++
 lib/riscv/setup.c           | 12 +++++
 riscv/Makefile              | 83 +++++++++++++++++++++++++++++++++
 riscv/cstart.S              | 92 +++++++++++++++++++++++++++++++++++++
 riscv/flat.lds              | 75 ++++++++++++++++++++++++++++++
 riscv/selftest.c            | 13 ++++++
 17 files changed, 469 insertions(+)
 create mode 100644 lib/riscv/.gitignore
 create mode 100644 lib/riscv/asm-offsets.c
 create mode 100644 lib/riscv/asm/asm-offsets.h
 create mode 100644 lib/riscv/asm/barrier.h
 create mode 100644 lib/riscv/asm/csr.h
 create mode 100644 lib/riscv/asm/io.h
 create mode 100644 lib/riscv/asm/page.h
 create mode 100644 lib/riscv/asm/setup.h
 create mode 100644 lib/riscv/asm/spinlock.h
 create mode 100644 lib/riscv/asm/stack.h
 create mode 100644 lib/riscv/io.c
 create mode 100644 lib/riscv/setup.c
 create mode 100644 riscv/Makefile
 create mode 100644 riscv/cstart.S
 create mode 100644 riscv/flat.lds
 create mode 100644 riscv/selftest.c
configure +14 -0
index ada6512702a1..05e6702eab06 100755
@@ -200,6 +200,11 @@ arch_name=$arch
 [ "$arch_name" = "arm64" ] && arch_name="aarch64"
 arch_libdir=$arch
 
+if [ "$arch" = "riscv" ]; then
+    echo "riscv32 or riscv64 must be specified"
+    exit 1
+fi
+
 if [ -z "$target" ]; then
     target="qemu"
 else
@@ -307,6 +312,9 @@ elif [ "$arch" = "ppc64" ]; then
         echo "You must provide endianness (big or little)!"
         usage
     fi
+elif [ "$arch" = "riscv32" ] || [ "$arch" = "riscv64" ]; then
+    testdir=riscv
+    arch_libdir=riscv
 else
     testdir=$arch
 fi
@@ -438,6 +446,12 @@ cat <<EOF >> lib/config.h
 #define CONFIG_ERRATA_FORCE ${errata_force}
 #define CONFIG_PAGE_SIZE _AC(${page_size}, UL)
 
+EOF
+elif [ "$arch" = "riscv32" ] || [ "$arch" = "riscv64" ]; then
+cat <<EOF >> lib/config.h
+
+#define CONFIG_UART_EARLY_BASE 0x10000000
+
 EOF
 fi
 echo "#endif" >> lib/config.h
lib/riscv/.gitignore new file +1 -0
new file mode 100644
index 000000000000..82da12e6bd4e
@@ -0,0 +1 @@
+/asm-offsets.[hs]
lib/riscv/asm-offsets.c new file +6 -0
new file mode 100644
index 000000000000..4a74df9e4a09
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+int main(void)
+{
+	return 0;
+}
lib/riscv/asm/asm-offsets.h new file +1 -0
new file mode 100644
index 000000000000..d370ee36a182
@@ -0,0 +1 @@
+#include <generated/asm-offsets.h>
lib/riscv/asm/barrier.h new file +13 -0
new file mode 100644
index 000000000000..c6a09066b2c7
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASMRISCV_BARRIER_H_
+#define _ASMRISCV_BARRIER_H_
+
+#define RISCV_FENCE(p, s) \
+	__asm__ __volatile__ ("fence " #p "," #s : : : "memory")
+
+/* These barriers need to enforce ordering on both devices or memory. */
+#define mb()		RISCV_FENCE(iorw,iorw)
+#define rmb()		RISCV_FENCE(ir,ir)
+#define wmb()		RISCV_FENCE(ow,ow)
+
+#endif /* _ASMRISCV_BARRIER_H_ */
lib/riscv/asm/csr.h new file +7 -0
new file mode 100644
index 000000000000..5c4f2de34f64
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASMRISCV_CSR_H_
+#define _ASMRISCV_CSR_H_
+
+#define CSR_SSCRATCH		0x140
+
+#endif /* _ASMRISCV_CSR_H_ */
lib/riscv/asm/io.h new file +78 -0

78 lines not on this page · raw message

lib/riscv/asm/page.h new file +7 -0
new file mode 100644
index 000000000000..7d7c9191605a
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASMRISCV_PAGE_H_
+#define _ASMRISCV_PAGE_H_
+
+#include <asm-generic/page.h>
+
+#endif /* _ASMRISCV_PAGE_H_ */
lib/riscv/asm/setup.h new file +7 -0
new file mode 100644
index 000000000000..385455f341cc
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASMRISCV_SETUP_H_
+#define _ASMRISCV_SETUP_H_
+
+void setup(const void *fdt, phys_addr_t freemem_start);
+
+#endif /* _ASMRISCV_SETUP_H_ */
lib/riscv/asm/spinlock.h new file +7 -0
new file mode 100644
index 000000000000..6e2b3009abf3
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASMRISCV_SPINLOCK_H_
+#define _ASMRISCV_SPINLOCK_H_
+
+#include <asm-generic/spinlock.h>
+
+#endif /* _ASMRISCV_SPINLOCK_H_ */
lib/riscv/asm/stack.h new file +9 -0
new file mode 100644
index 000000000000..d081d0716d7b
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASMRISCV_STACK_H_
+#define _ASMRISCV_STACK_H_
+
+#ifndef _STACK_H_
+#error Do not directly include <asm/stack.h>. Just use <stack.h>.
+#endif
+
+#endif
lib/riscv/io.c new file +44 -0

44 lines not on this page · raw message

lib/riscv/setup.c new file +12 -0
new file mode 100644
index 000000000000..8937525ccb7f
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Initialize machine setup information and I/O.
+ *
+ * Copyright (C) 2023, Ventana Micro Systems Inc., Andrew Jones <[email protected]>
+ */
+#include <libcflat.h>
+#include <asm/setup.h>
+
+void setup(const void *fdt, phys_addr_t freemem_start)
+{
+}
riscv/Makefile new file +83 -0

83 lines not on this page · raw message

riscv/cstart.S new file +92 -0

92 lines not on this page · raw message

riscv/flat.lds new file +75 -0

75 lines not on this page · raw message

riscv/selftest.c new file +13 -0
new file mode 100644
index 000000000000..88afa732649e
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Test the framework itself. These tests confirm that setup works.
+ *
+ * Copyright (C) 2023, Ventana Micro Systems Inc., Andrew Jones <[email protected]>
+ */
+#include <libcflat.h>
+
+int main(void)
+{
+	puts("Hello, world\n");
+	return 0;
+}
Signature · 1 line
2.43.0
#4 lore raw

PATCH 3/24 arm/arm64: Move cpumask.h to common lib

In reply to Andrew Jones

RISC-V will also make use of cpumask.h, so move it to the arch-common directory.

---
 lib/arm/asm/gic-v2.h        | 2 +-
 lib/arm/asm/gic-v3.h        | 2 +-
 lib/arm/asm/gic.h           | 2 +-
 lib/arm/asm/smp.h           | 2 +-
 lib/arm/mmu.c               | 2 +-
 lib/arm/smp.c               | 2 +-
 lib/arm64/asm/cpumask.h     | 1 -
 lib/{arm/asm => }/cpumask.h | 9 ++++-----
 8 files changed, 10 insertions(+), 12 deletions(-)
 delete mode 100644 lib/arm64/asm/cpumask.h
 rename lib/{arm/asm => }/cpumask.h (94%)
lib/arm/asm/gic-v2.h +1 -1
index 1fcfd43c8075..ff11afb15d30 100644
@@ -18,7 +18,7 @@
 #define GICC_IAR_INT_ID_MASK		0x3ff
 
 #ifndef __ASSEMBLY__
-#include <asm/cpumask.h>
+#include <cpumask.h>
 
 struct gicv2_data {
 	void *dist_base;
lib/arm/asm/gic-v3.h +1 -1
index b4ce130e56c6..a1cc62a298b8 100644
@@ -67,10 +67,10 @@
 #include <asm/arch_gicv3.h>
 
 #ifndef __ASSEMBLY__
+#include <cpumask.h>
 #include <asm/setup.h>
 #include <asm/processor.h>
 #include <asm/delay.h>
-#include <asm/cpumask.h>
 #include <asm/smp.h>
 #include <asm/io.h>
 
lib/arm/asm/gic.h +1 -1
index 189840014b02..dc8cc18c0fbd 100644
@@ -47,7 +47,7 @@
 #define SPI(irq)			((irq) + GIC_FIRST_SPI)
 
 #ifndef __ASSEMBLY__
-#include <asm/cpumask.h>
+#include <cpumask.h>
 
 enum gic_irq_state {
 	GIC_IRQ_STATE_INACTIVE,
lib/arm/asm/smp.h +1 -1
index dee4c1a883e7..bb3e71a55e8c 100644
@@ -5,8 +5,8 @@
  *
  * This work is licensed under the terms of the GNU LGPL, version 2.
  */
+#include <cpumask.h>
 #include <asm/thread_info.h>
-#include <asm/cpumask.h>
 
 #define smp_processor_id()		(current_thread_info()->cpu)
 
lib/arm/mmu.c +1 -1
index 2f4ec815a35d..b16517a3200d 100644
@@ -5,9 +5,9 @@
  *
  * This work is licensed under the terms of the GNU LGPL, version 2.
  */
+#include <cpumask.h>
 #include <asm/setup.h>
 #include <asm/thread_info.h>
-#include <asm/cpumask.h>
 #include <asm/mmu.h>
 #include <asm/setup.h>
 #include <asm/page.h>
lib/arm/smp.c +1 -1
index 1d470d1aab45..78fc1656cefa 100644
@@ -7,9 +7,9 @@
  */
 #include <libcflat.h>
 #include <auxinfo.h>
+#include <cpumask.h>
 #include <asm/thread_info.h>
 #include <asm/spinlock.h>
-#include <asm/cpumask.h>
 #include <asm/barrier.h>
 #include <asm/mmu.h>
 #include <asm/psci.h>
lib/arm64/asm/cpumask.h deleted +0 -1
deleted file mode 100644
index d1421e7abe31..000000000000
@@ -1 +0,0 @@
-#include "../../arm/asm/cpumask.h"
lib/cpumask.h renamed from lib/arm/asm/cpumask.h +4 -5
similarity index 94%
rename from lib/arm/asm/cpumask.h
rename to lib/cpumask.h
index 3fa57bfb17c6..d30e14cda09e 100644
@@ -1,12 +1,11 @@
-#ifndef _ASMARM_CPUMASK_H_
-#define _ASMARM_CPUMASK_H_
+/* SPDX-License-Identifier: GPL-2.0-only */
 /*
  * Simple cpumask implementation
  *
  * Copyright (C) 2015, Red Hat Inc, Andrew Jones <[email protected]>
- *
- * This work is licensed under the terms of the GNU LGPL, version 2.
  */
+#ifndef _CPUMASK_H_
+#define _CPUMASK_H_
 #include <asm/setup.h>
 #include <bitops.h>
 
@@ -120,4 +119,4 @@ static inline int cpumask_next(int cpu, const cpumask_t *mask)
 			(cpu) < nr_cpus; 			\
 			(cpu) = cpumask_next(cpu, mask))
 
-#endif /* _ASMARM_CPUMASK_H_ */
+#endif /* _CPUMASK_H_ */
Signature · 1 line
2.43.0
Reviewed-by on v1 3/24
#5 lore raw

Re: [kvm-unit-tests PATCH 03/24] arm/arm64: Move cpumask.h to common lib

Quoted 14 lines · Andrew Jones

RISC-V will also make use of cpumask.h, so move it to the arch-common directory.

---
  lib/arm/asm/gic-v2.h        | 2 +-
  lib/arm/asm/gic-v3.h        | 2 +-
  lib/arm/asm/gic.h           | 2 +-
  lib/arm/asm/smp.h           | 2 +-
  lib/arm/mmu.c               | 2 +-
  lib/arm/smp.c               | 2 +-
  lib/arm64/asm/cpumask.h     | 1 -
  lib/{arm/asm => }/cpumask.h | 9 ++++-----
  8 files changed, 10 insertions(+), 12 deletions(-)
Quoted 2 lines

delete mode 100644 lib/arm64/asm/cpumask.h
rename lib/{arm/asm => }/cpumask.h (94%)

#6 lore raw

PATCH 4/24 arm/arm64: Share cpu online, present and idle masks

In reply to Andrew Jones

RISC-V will also use Arm's three cpumasks. These were in smp.h, but they can be in cpumask.h instead, so move them there, which is now shared.

---
 lib/arm/asm/smp.h | 33 ---------------------------------
 lib/cpumask.h     | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 33 deletions(-)
lib/arm/asm/smp.h +0 -33
index bb3e71a55e8c..b89a68dd344f 100644
@@ -23,39 +23,6 @@ extern bool cpu0_calls_idle;
 extern void halt(void);
 extern void do_idle(void);
 
-extern cpumask_t cpu_present_mask;
-extern cpumask_t cpu_online_mask;
-extern cpumask_t cpu_idle_mask;
-#define cpu_present(cpu)		cpumask_test_cpu(cpu, &cpu_present_mask)
-#define cpu_online(cpu)			cpumask_test_cpu(cpu, &cpu_online_mask)
-#define cpu_idle(cpu)			cpumask_test_cpu(cpu, &cpu_idle_mask)
-#define for_each_present_cpu(cpu)	for_each_cpu(cpu, &cpu_present_mask)
-#define for_each_online_cpu(cpu)	for_each_cpu(cpu, &cpu_online_mask)
-
-static inline void set_cpu_present(int cpu, bool present)
-{
-	if (present)
-		cpumask_set_cpu(cpu, &cpu_present_mask);
-	else
-		cpumask_clear_cpu(cpu, &cpu_present_mask);
-}
-
-static inline void set_cpu_online(int cpu, bool online)
-{
-	if (online)
-		cpumask_set_cpu(cpu, &cpu_online_mask);
-	else
-		cpumask_clear_cpu(cpu, &cpu_online_mask);
-}
-
-static inline void set_cpu_idle(int cpu, bool idle)
-{
-	if (idle)
-		cpumask_set_cpu(cpu, &cpu_idle_mask);
-	else
-		cpumask_clear_cpu(cpu, &cpu_idle_mask);
-}
-
 extern void smp_boot_secondary(int cpu, secondary_entry_fn entry);
 extern void on_cpu_async(int cpu, void (*func)(void *data), void *data);
 extern void on_cpu(int cpu, void (*func)(void *data), void *data);
lib/cpumask.h +33 -0
index d30e14cda09e..be1919234d8e 100644
@@ -119,4 +119,37 @@ static inline int cpumask_next(int cpu, const cpumask_t *mask)
 			(cpu) < nr_cpus; 			\
 			(cpu) = cpumask_next(cpu, mask))
 
+extern cpumask_t cpu_present_mask;
+extern cpumask_t cpu_online_mask;
+extern cpumask_t cpu_idle_mask;
+#define cpu_present(cpu)		cpumask_test_cpu(cpu, &cpu_present_mask)
+#define cpu_online(cpu)			cpumask_test_cpu(cpu, &cpu_online_mask)
+#define cpu_idle(cpu)			cpumask_test_cpu(cpu, &cpu_idle_mask)
+#define for_each_present_cpu(cpu)	for_each_cpu(cpu, &cpu_present_mask)
+#define for_each_online_cpu(cpu)	for_each_cpu(cpu, &cpu_online_mask)
+
+static inline void set_cpu_present(int cpu, bool present)
+{
+	if (present)
+		cpumask_set_cpu(cpu, &cpu_present_mask);
+	else
+		cpumask_clear_cpu(cpu, &cpu_present_mask);
+}
+
+static inline void set_cpu_online(int cpu, bool online)
+{
+	if (online)
+		cpumask_set_cpu(cpu, &cpu_online_mask);
+	else
+		cpumask_clear_cpu(cpu, &cpu_online_mask);
+}
+
+static inline void set_cpu_idle(int cpu, bool idle)
+{
+	if (idle)
+		cpumask_set_cpu(cpu, &cpu_idle_mask);
+	else
+		cpumask_clear_cpu(cpu, &cpu_idle_mask);
+}
+
 #endif /* _CPUMASK_H_ */
Signature · 1 line
2.43.0
Reviewed-by on v1 4/24
#7 lore raw

Re: [kvm-unit-tests PATCH 04/24] arm/arm64: Share cpu online, present and idle masks

Quoted 9 lines · Andrew Jones

RISC-V will also use Arm's three cpumasks. These were in smp.h, but they can be in cpumask.h instead, so move them there, which is now shared.

---
  lib/arm/asm/smp.h | 33 ---------------------------------
  lib/cpumask.h     | 33 +++++++++++++++++++++++++++++++++
  2 files changed, 33 insertions(+), 33 deletions(-)
#8 lore raw

PATCH 5/24 riscv: Add DT parsing

In reply to Andrew Jones

Start building setup() by copying code from Arm and adding dependencies along the way like bitops and a few more barriers. We now parse the DT for the UART base address to be sure we find what we expect with the early base. We also parse the CPU nodes to get the hartids, even though we don't yet support booting secondaries. Finally, add parsing of bootargs to get the command line and parsing of the environ to set the environment variables, and then extend the selftest to ensure it all works.

We don't do proper memory setup yet, only just enough to allocate memory for the environment variables and any another small mallocs that may be necessary.

Note, we've added a banner, which Arm doesn't have, because we need to separate the test output from OpenSBI output.

Run with

  qemu-system-riscv64 -nographic -M virt \
      -kernel riscv/selftest.flat \
      -append 'foo bar baz' \
      -initrd test-env \
      -smp 16

where test-env is a text file with the environment, i.e.

$ cat test-env
FOO=foo
BAR=bar
BAZ=baz
---
 lib/linux/const.h       |   2 +
 lib/riscv/asm/barrier.h |   5 ++
 lib/riscv/asm/bitops.h  |  21 ++++++++
 lib/riscv/asm/csr.h     |  64 +++++++++++++++++++++++
 lib/riscv/asm/setup.h   |   7 +++
 lib/riscv/bitops.c      |  47 +++++++++++++++++
 lib/riscv/io.c          |  51 +++++++++++++++++++
 lib/riscv/setup.c       | 109 ++++++++++++++++++++++++++++++++++++++++
 lib/riscv/smp.c         |   6 +++
 riscv/Makefile          |   5 ++
 riscv/selftest.c        |  44 ++++++++++++++--
 11 files changed, 358 insertions(+), 3 deletions(-)
 create mode 100644 lib/riscv/asm/bitops.h
 create mode 100644 lib/riscv/bitops.c
 create mode 100644 lib/riscv/smp.c
lib/linux/const.h +2 -0
index be114dc4a553..f622fa852ced 100644
@@ -15,10 +15,12 @@
 #if defined(__ASSEMBLY__) || defined(__ASSEMBLER__)
 #define _AC(X,Y)	X
 #define _AT(T,X)	X
+#define __ASM_STR(X)	X
 #else
 #define __AC(X,Y)	(X##Y)
 #define _AC(X,Y)	__AC(X,Y)
 #define _AT(T,X)	((T)(X))
+#define __ASM_STR(X)	#X
 #endif
 
 #define _BITUL(x)	(_AC(1,UL) << (x))
lib/riscv/asm/barrier.h +5 -0
index c6a09066b2c7..6036d66af76f 100644
@@ -10,4 +10,9 @@
 #define rmb()		RISCV_FENCE(ir,ir)
 #define wmb()		RISCV_FENCE(ow,ow)
 
+/* These barriers do not need to enforce ordering on devices, just memory. */
+#define smp_mb()	RISCV_FENCE(rw,rw)
+#define smp_rmb()	RISCV_FENCE(r,r)
+#define smp_wmb()	RISCV_FENCE(w,w)
+
 #endif /* _ASMRISCV_BARRIER_H_ */
lib/riscv/asm/bitops.h new file +21 -0
new file mode 100644
index 000000000000..0d982507c7e7
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASMRISCV_BITOPS_H_
+#define _ASMRISCV_BITOPS_H_
+
+#ifndef _BITOPS_H_
+#error only <bitops.h> can be included directly
+#endif
+
+#ifdef CONFIG_64BIT
+#define BITS_PER_LONG	64
+#else
+#define BITS_PER_LONG	32
+#endif
+
+void set_bit(int nr, volatile unsigned long *addr);
+void clear_bit(int nr, volatile unsigned long *addr);
+int test_bit(int nr, const volatile unsigned long *addr);
+int test_and_set_bit(int nr, volatile unsigned long *addr);
+int test_and_clear_bit(int nr, volatile unsigned long *addr);
+
+#endif /* _ASMRISCV_BITOPS_H_ */
lib/riscv/asm/csr.h +64 -0

71 lines not on this page · raw message

lib/riscv/asm/setup.h +7 -0
index 385455f341cc..c8cfebb4f2c1 100644
@@ -1,7 +1,14 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 #ifndef _ASMRISCV_SETUP_H_
 #define _ASMRISCV_SETUP_H_
+#include <libcflat.h>
 
+#define NR_CPUS 16
+extern unsigned long cpus[NR_CPUS];       /* per-cpu IDs (hartids) */
+extern int nr_cpus;
+int hartid_to_cpu(unsigned long hartid);
+
+void io_init(void);
 void setup(const void *fdt, phys_addr_t freemem_start);
 
 #endif /* _ASMRISCV_SETUP_H_ */
lib/riscv/bitops.c new file +47 -0

47 lines not on this page · raw message

lib/riscv/io.c +51 -0

64 lines not on this page · raw message

lib/riscv/setup.c +109 -0

117 lines not on this page · raw message

lib/riscv/smp.c new file +6 -0
new file mode 100644
index 000000000000..a89b59d8dd03
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <cpumask.h>
+
+cpumask_t cpu_present_mask;
+cpumask_t cpu_online_mask;
+cpumask_t cpu_idle_mask;
riscv/Makefile +5 -0
index f2e89f0e4c38..ddf2a0e016a8 100644
@@ -20,8 +20,13 @@ $(TEST_DIR)/sieve.elf: AUXFLAGS = 0x1
 
 cstart.o = $(TEST_DIR)/cstart.o
 
+cflatobjs += lib/alloc.o
+cflatobjs += lib/alloc_phys.o
+cflatobjs += lib/devicetree.o
+cflatobjs += lib/riscv/bitops.o
 cflatobjs += lib/riscv/io.o
 cflatobjs += lib/riscv/setup.o
+cflatobjs += lib/riscv/smp.o
 
 ########################################
 
riscv/selftest.c +41 -3

50 lines not on this page · raw message

Signature · 1 line
2.43.0
#9 lore raw

PATCH 6/24 riscv: Add initial SBI support

In reply to Andrew Jones

Add an SBI call function and immediately apply it to properly exiting the test (instead of hanging) by invoking SBI shutdown from exit(). Also seed an SBI test file with a simple SBI test that checks mvendorid is correctly extracted.

Run with e.g.

  qemu-system-riscv64 -nographic -M virt \
      -kernel riscv/sbi.flat \
      -cpu rv64,mvendorid=45 \
      -initrd sbi-env

and be happy that ctrl-a c q is no longer necessary to return to the shell prompt. sbi-env has MVENDORID=45 in it.

---
 lib/riscv/asm/sbi.h | 32 ++++++++++++++++++++++++++++++++
 lib/riscv/io.c      |  2 ++
 lib/riscv/sbi.c     | 35 +++++++++++++++++++++++++++++++++++
 riscv/Makefile      |  2 ++
 riscv/sbi.c         | 41 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 112 insertions(+)
 create mode 100644 lib/riscv/asm/sbi.h
 create mode 100644 lib/riscv/sbi.c
 create mode 100644 riscv/sbi.c
lib/riscv/asm/sbi.h new file +32 -0
new file mode 100644
index 000000000000..aeff07f6f1a8
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASMRISCV_SBI_H_
+#define _ASMRISCV_SBI_H_
+
+enum sbi_ext_id {
+	SBI_EXT_BASE = 0x10,
+	SBI_EXT_SRST = 0x53525354,
+};
+
+enum sbi_ext_base_fid {
+	SBI_EXT_BASE_GET_SPEC_VERSION = 0,
+	SBI_EXT_BASE_GET_IMP_ID,
+	SBI_EXT_BASE_GET_IMP_VERSION,
+	SBI_EXT_BASE_PROBE_EXT,
+	SBI_EXT_BASE_GET_MVENDORID,
+	SBI_EXT_BASE_GET_MARCHID,
+	SBI_EXT_BASE_GET_MIMPID,
+};
+
+struct sbiret {
+	long error;
+	long value;
+};
+
+struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
+			unsigned long arg1, unsigned long arg2,
+			unsigned long arg3, unsigned long arg4,
+			unsigned long arg5);
+
+void sbi_shutdown(void);
+
+#endif /* _ASMRISCV_SBI_H_ */
lib/riscv/io.c +2 -0
index aeda74be61ee..b3f587bb68ca 100644
@@ -9,6 +9,7 @@
 #include <config.h>
 #include <devicetree.h>
 #include <asm/io.h>
+#include <asm/sbi.h>
 #include <asm/setup.h>
 #include <asm/spinlock.h>
 
@@ -90,6 +91,7 @@ void halt(int code);
 void exit(int code)
 {
 	printf("\nEXIT: STATUS=%d\n", ((code) << 1) | 1);
+	sbi_shutdown();
 	halt(code);
 	__builtin_unreachable();
 }
lib/riscv/sbi.c new file +35 -0
new file mode 100644
index 000000000000..fd758555b888
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <libcflat.h>
+#include <asm/sbi.h>
+
+struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
+			unsigned long arg1, unsigned long arg2,
+			unsigned long arg3, unsigned long arg4,
+			unsigned long arg5)
+{
+	register uintptr_t a0 asm ("a0") = (uintptr_t)(arg0);
+	register uintptr_t a1 asm ("a1") = (uintptr_t)(arg1);
+	register uintptr_t a2 asm ("a2") = (uintptr_t)(arg2);
+	register uintptr_t a3 asm ("a3") = (uintptr_t)(arg3);
+	register uintptr_t a4 asm ("a4") = (uintptr_t)(arg4);
+	register uintptr_t a5 asm ("a5") = (uintptr_t)(arg5);
+	register uintptr_t a6 asm ("a6") = (uintptr_t)(fid);
+	register uintptr_t a7 asm ("a7") = (uintptr_t)(ext);
+	struct sbiret ret;
+
+	asm volatile (
+		"ecall"
+		: "+r" (a0), "+r" (a1)
+		: "r" (a2), "r" (a3), "r" (a4), "r" (a5), "r" (a6), "r" (a7)
+		: "memory");
+	ret.error = a0;
+	ret.value = a1;
+
+	return ret;
+}
+
+void sbi_shutdown(void)
+{
+	sbi_ecall(SBI_EXT_SRST, 0, 0, 0, 0, 0, 0, 0);
+	puts("SBI shutdown failed!\n");
+}
riscv/Makefile +2 -0
index ddf2a0e016a8..4e7fcc538ba1 100644
@@ -11,6 +11,7 @@ exe = flat
 endif
 
 tests =
+tests += $(TEST_DIR)/sbi.$(exe)
 tests += $(TEST_DIR)/selftest.$(exe)
 #tests += $(TEST_DIR)/sieve.$(exe)
 
@@ -25,6 +26,7 @@ cflatobjs += lib/alloc_phys.o
 cflatobjs += lib/devicetree.o
 cflatobjs += lib/riscv/bitops.o
 cflatobjs += lib/riscv/io.o
+cflatobjs += lib/riscv/sbi.o
 cflatobjs += lib/riscv/setup.o
 cflatobjs += lib/riscv/smp.o
 
riscv/sbi.c new file +41 -0

41 lines not on this page · raw message

Signature · 1 line
2.43.0
#10 lore raw

PATCH 7/24 riscv: Add run script and unittests.cfg

In reply to Andrew Jones

With a run script and a unittests.cfg file we can now run

 $ KVM_UNIT_TESTS_ENV=test-env ./run_tests.sh

where test-env has the environment variables needed for both tests.

Note, to change the SBI implementation under test, for example to RustSBI, QEMU needs the -bios parameter. The full command line would be

  qemu-system-riscv64 -nographic -M virt -cpu rv64 \
      -kernel riscv/sbi.flat \
      -bios $PATH_TO_RUSTSBI

and with the run script, it's

SBI_IMPL="-bios $PATH_TO_RUSTSBI" ./run_tests.sh -g sbi

(note the '-g sbi' to only run the SBI test group)

Finally, with the addition of the run script, 'make standalone' now also works.

---
 riscv/run           | 38 ++++++++++++++++++++++++++++++++++++++
 riscv/unittests.cfg | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+)
 create mode 100755 riscv/run
 create mode 100644 riscv/unittests.cfg
riscv/run new file +38 -0
new file mode 100755
index 000000000000..8a189e5349a2
@@ -0,0 +1,38 @@
+#!/usr/bin/env bash
+
+if [ -z "$KUT_STANDALONE" ]; then
+	if [ ! -f config.mak ]; then
+		echo "run ./configure && make first. See ./configure -h"
+		exit 2
+	fi
+	source config.mak
+	source scripts/arch-run.bash
+fi
+
+if [[ $PROCESSOR == "riscv32" ]]; then
+	processor="rv32"
+elif [[ $PROCESSOR == "riscv64" ]]; then
+	processor="rv64"
+fi
+
+set_qemu_accelerator || exit $?
+[ "$ACCEL" = "kvm" ] && QEMU_ARCH=$HOST
+acc="-accel $ACCEL$ACCEL_PROPS"
+
+qemu=$(search_qemu_binary) || exit $?
+if ! $qemu -machine '?' | grep -q 'RISC-V VirtIO board'; then
+	echo "$qemu doesn't support mach-virt ('-machine virt'). Exiting."
+	exit 2
+fi
+mach='-machine virt'
+
+command="$qemu -nodefaults -nographic -serial mon:stdio"
+command+=" $mach $acc -cpu $processor "
+command="$(migration_cmd) $(timeout_cmd) $command"
+
+if [ "$EFI_RUN" = "y" ]; then
+	ENVIRON_DEFAULT=n run_qemu_status $command "$@"
+else
+	# We return the exit code via stdout, not via the QEMU return code
+	run_qemu_status $command -kernel "$@"
+fi
riscv/unittests.cfg new file +37 -0
new file mode 100644
index 000000000000..3840136064ee
@@ -0,0 +1,37 @@
+##############################################################################
+# unittest configuration
+#
+# [unittest_name]
+# file = <name>.flat		# Name of the flat file to be used.
+# smp  = <num>			# Number of processors the VM will use
+#				# during this test. Use $MAX_SMP to use
+#				# the maximum the host supports. Defaults
+#				# to one.
+# extra_params = -append <params...>	# Additional parameters used.
+# arch = riscv32|riscv64		# Select one if the test case is
+#					# specific to only one.
+# groups = <group_name1> <group_name2> ...	# Used to identify test cases
+#						# with run_tests -g ...
+#						# Specify group_name=nodefault
+#						# to have test not run by
+#						# default
+# accel = kvm|tcg		# Optionally specify if test must run with
+#				# kvm or tcg. If not specified, then kvm will
+#				# be used when available.
+# timeout = <duration>		# Optionally specify a timeout.
+# check = <path>=<value> # check a file for a particular value before running
+#                        # a test. The check line can contain multiple files
+#                        # to check separated by a space but each check
+#                        # parameter needs to be of the form <path>=<value>
+##############################################################################
+
+[selftest]
+file = selftest.flat
+smp = 16
+extra_params = -append 'foo bar baz'
+groups = selftest
+
+[sbi]
+file = sbi.flat
+extra_params = $SBI_IMPL
+groups = sbi
Signature · 1 line
2.43.0
#11 lore raw

PATCH 8/24 riscv: Add riscv32 support

In reply to Andrew Jones

Make a few tweaks to allow also building and running riscv32.

---
 lib/elf.h               | 11 +++++++++++
 lib/ldiv32.c            | 16 ++++++++++++++++
 lib/riscv/asm-offsets.c | 11 +++++++++++
 riscv/Makefile          |  3 +++
 riscv/cstart.S          | 35 +++++++++++++++++++++--------------
 5 files changed, 62 insertions(+), 14 deletions(-)
lib/elf.h +11 -0
index abd5cf4beaad..7a7db57774cd 100644
@@ -8,6 +8,11 @@
 
 #include <libcflat.h>
 
+/* 32-bit ELF base types. */
+typedef u32	Elf32_Addr;
+typedef u32	Elf32_Xword;
+typedef s32	Elf32_Sxword;
+
 /* 64-bit ELF base types. */
 typedef u64	Elf64_Addr;
 typedef u64	Elf64_Xword;
@@ -26,6 +31,12 @@ typedef struct elf64_rel {
 	Elf64_Xword r_info;     /* index and type of relocation */
 } Elf64_Rel;
 
+typedef struct elf32_rela {
+	Elf32_Addr r_offset;    /* Location at which to apply the action */
+	Elf32_Xword r_info;     /* index and type of relocation */
+	Elf32_Sxword r_addend;  /* Constant addend used to compute value */
+} Elf32_Rela;
+
 typedef struct elf64_rela {
 	Elf64_Addr r_offset;    /* Location at which to apply the action */
 	Elf64_Xword r_info;     /* index and type of relocation */
lib/ldiv32.c +16 -0
index 897a4b9cd39e..9ce2a6a1faf0 100644
@@ -1,5 +1,21 @@
 #include <stdint.h>
 
+#if __riscv_xlen == 32
+int __clzdi2(unsigned long);
+
+int __clzdi2(unsigned long a)
+{
+	int n = 0;
+
+	while (a) {
+		++n;
+		a >>= 1;
+	}
+
+	return 32 - n;
+}
+#endif
+
 extern uint64_t __udivmoddi4(uint64_t num, uint64_t den, uint64_t *p_rem);
 extern int64_t __divmoddi4(int64_t num, int64_t den, int64_t *p_rem);
 extern int64_t __moddi3(int64_t num, int64_t den);
lib/riscv/asm-offsets.c +11 -0
index 4a74df9e4a09..eb337b7547b8 100644
@@ -1,6 +1,17 @@
 // SPDX-License-Identifier: GPL-2.0-only
+#include <kbuild.h>
+#include <elf.h>
 
 int main(void)
 {
+#if __riscv_xlen == 32
+	OFFSET(ELF_RELA_OFFSET, elf32_rela, r_offset);
+	OFFSET(ELF_RELA_ADDEND, elf32_rela, r_addend);
+	DEFINE(ELF_RELA_SIZE, sizeof(struct elf32_rela));
+#elif __riscv_xlen == 64
+	OFFSET(ELF_RELA_OFFSET, elf64_rela, r_offset);
+	OFFSET(ELF_RELA_ADDEND, elf64_rela, r_addend);
+	DEFINE(ELF_RELA_SIZE, sizeof(struct elf64_rela));
+#endif
 	return 0;
 }
riscv/Makefile +3 -0
index 4e7fcc538ba1..fb97e678a456 100644
@@ -29,6 +29,9 @@ cflatobjs += lib/riscv/io.o
 cflatobjs += lib/riscv/sbi.o
 cflatobjs += lib/riscv/setup.o
 cflatobjs += lib/riscv/smp.o
+ifeq ($(ARCH),riscv32)
+cflatobjs += lib/ldiv32.o
+endif
 
 ########################################
 
riscv/cstart.S +21 -14
index a28d75e8021e..6ec2231e5812 100644
@@ -4,11 +4,23 @@
  *
  * Copyright (C) 2023, Ventana Micro Systems Inc., Andrew Jones <[email protected]>
  */
+#include <asm/asm-offsets.h>
 #include <asm/csr.h>
 
+#if __riscv_xlen == 64
+#define __REG_SEL(a, b) a
+#elif __riscv_xlen == 32
+#define __REG_SEL(a, b) b
+#else
+#error "Unexpected __riscv_xlen"
+#endif
+
+#define REG_L	__REG_SEL(ld, lw)
+#define REG_S	__REG_SEL(sd, sw)
+
 .macro zero_range, tmp1, tmp2
 9998:	beq	\tmp1, \tmp2, 9997f
-	sd	zero, 0(\tmp1)
+	REG_S	zero, 0(\tmp1)
 	addi	\tmp1, \tmp1, 8
 	j	9998b
 9997:
@@ -33,26 +45,20 @@ start:
 
 	/*
 	 * Update all R_RISCV_RELATIVE relocations using the table
-	 * of Elf64_Rela entries between reloc_start/end. The build
-	 * will not emit other relocation types.
-	 *
-	 * struct Elf64_Rela {
-	 * 	uint64_t r_offset;
-	 * 	uint64_t r_info;
-	 * 	int64_t  r_addend;
-	 * }
+	 * of Elf32_Rela/Elf64_Rela entries between reloc_start/end.
+	 * The build will not emit other relocation types.
 	 */
 	la	a1, reloc_start
 	la	a2, reloc_end
 	la	a3, start			// base
 1:
 	bge	a1, a2, 1f
-	ld	a4, 0(a1)			// r_offset
-	ld	a5, 16(a1)			// r_addend
+	REG_L	a4, ELF_RELA_OFFSET(a1)		// r_offset
+	REG_L	a5, ELF_RELA_ADDEND(a1)		// r_addend
 	add	a4, a3, a4			// addr = base + r_offset
 	add	a5, a3, a5			// val = base + r_addend
-	sd	a5, 0(a4)			// *addr = val
-	addi	a1, a1, 24
+	REG_S	a5, 0(a4)			// *addr = val
+	addi	a1, a1, ELF_RELA_SIZE
 	j	1b
 
 1:
@@ -72,11 +78,12 @@ start:
 
 	/* complete setup */
 	la	a1, stacktop			// a1 is the base of free memory
+	mv	a2, zero			// clear a2 for xlen=32
 	call	setup				// a0 is the addr of the dtb
 
 	/* run the test */
 	la	a0, __argc
-	ld	a0, 0(a0)
+	REG_L	a0, 0(a0)
 	la	a1, __argv
 	la	a2, __environ
 	call	main
Signature · 1 line
2.43.0
#12 lore raw

PATCH 9/24 riscv: Add exception handling

In reply to Andrew Jones

Steal more code from Linux to implement exception handling, but with the same kvm-unit-tests API that Arm has. Also introduce struct thread_info like Arm has in order to hold the handler pointers. Finally, as usual, extend the selftest to make sure it all works.

---
 lib/riscv/asm-offsets.c   |  38 +++++++++++++
 lib/riscv/asm/bug.h       |  20 +++++++
 lib/riscv/asm/csr.h       |  12 ++++
 lib/riscv/asm/processor.h |  26 +++++++++
 lib/riscv/asm/ptrace.h    |  46 +++++++++++++++
 lib/riscv/asm/setup.h     |   3 +-
 lib/riscv/processor.c     |  60 ++++++++++++++++++++
 lib/riscv/setup.c         |   9 ++-
 riscv/Makefile            |   1 +
 riscv/cstart.S            | 116 +++++++++++++++++++++++++++++++++++++-
 riscv/selftest.c          |  20 ++++++-
 11 files changed, 343 insertions(+), 8 deletions(-)
 create mode 100644 lib/riscv/asm/bug.h
 create mode 100644 lib/riscv/asm/processor.h
 create mode 100644 lib/riscv/asm/ptrace.h
 create mode 100644 lib/riscv/processor.c
lib/riscv/asm-offsets.c +38 -0
index eb337b7547b8..7b88d16fd0e4 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 #include <kbuild.h>
 #include <elf.h>
+#include <asm/ptrace.h>
 
 int main(void)
 {
@@ -13,5 +14,42 @@ int main(void)
 	OFFSET(ELF_RELA_ADDEND, elf64_rela, r_addend);
 	DEFINE(ELF_RELA_SIZE, sizeof(struct elf64_rela));
 #endif
+	OFFSET(PT_EPC, pt_regs, epc);
+	OFFSET(PT_RA, pt_regs, ra);
+	OFFSET(PT_SP, pt_regs, sp);
+	OFFSET(PT_GP, pt_regs, gp);
+	OFFSET(PT_TP, pt_regs, tp);
+	OFFSET(PT_T0, pt_regs, t0);
+	OFFSET(PT_T1, pt_regs, t1);
+	OFFSET(PT_T2, pt_regs, t2);
+	OFFSET(PT_S0, pt_regs, s0);
+	OFFSET(PT_S1, pt_regs, s1);
+	OFFSET(PT_A0, pt_regs, a0);
+	OFFSET(PT_A1, pt_regs, a1);
+	OFFSET(PT_A2, pt_regs, a2);
+	OFFSET(PT_A3, pt_regs, a3);
+	OFFSET(PT_A4, pt_regs, a4);
+	OFFSET(PT_A5, pt_regs, a5);
+	OFFSET(PT_A6, pt_regs, a6);
+	OFFSET(PT_A7, pt_regs, a7);
+	OFFSET(PT_S2, pt_regs, s2);
+	OFFSET(PT_S3, pt_regs, s3);
+	OFFSET(PT_S4, pt_regs, s4);
+	OFFSET(PT_S5, pt_regs, s5);
+	OFFSET(PT_S6, pt_regs, s6);
+	OFFSET(PT_S7, pt_regs, s7);
+	OFFSET(PT_S8, pt_regs, s8);
+	OFFSET(PT_S9, pt_regs, s9);
+	OFFSET(PT_S10, pt_regs, s10);
+	OFFSET(PT_S11, pt_regs, s11);
+	OFFSET(PT_T3, pt_regs, t3);
+	OFFSET(PT_T4, pt_regs, t4);
+	OFFSET(PT_T5, pt_regs, t5);
+	OFFSET(PT_T6, pt_regs, t6);
+	OFFSET(PT_STATUS, pt_regs, status);
+	OFFSET(PT_BADADDR, pt_regs, badaddr);
+	OFFSET(PT_CAUSE, pt_regs, cause);
+	OFFSET(PT_ORIG_A0, pt_regs, orig_a0);
+	DEFINE(PT_SIZE, sizeof(struct pt_regs));
 	return 0;
 }
lib/riscv/asm/bug.h new file +20 -0
new file mode 100644
index 000000000000..a6f4136ba1b6
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASMRISCV_BUG_H_
+#define _ASMRISCV_BUG_H_
+
+#ifndef __ASSEMBLY__
+
+static inline void bug(void)
+{
+	asm volatile("ebreak");
+}
+
+#else
+
+.macro bug
+	ebreak
+.endm
+
+#endif
+
+#endif /* _ASMRISCV_BUG_H_ */
lib/riscv/asm/csr.h +12 -0
index 356ae054bfff..eeddd1fb448a 100644
@@ -3,7 +3,19 @@
 #define _ASMRISCV_CSR_H_
 #include <linux/const.h>
 
+#define CSR_SSTATUS		0x100
+#define CSR_STVEC		0x105
 #define CSR_SSCRATCH		0x140
+#define CSR_SEPC		0x141
+#define CSR_SCAUSE		0x142
+#define CSR_STVAL		0x143
+
+/* Exception cause high bit - is an interrupt if set */
+#define CAUSE_IRQ_FLAG		(_AC(1, UL) << (__riscv_xlen - 1))
+
+/* Exception causes */
+#define EXC_INST_ILLEGAL	2
+#define EXC_BREAKPOINT		3
 
 #ifndef __ASSEMBLY__
 
lib/riscv/asm/processor.h new file +26 -0
new file mode 100644
index 000000000000..d8b7018c9102
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASMRISCV_PROCESSOR_H_
+#define _ASMRISCV_PROCESSOR_H_
+#include <asm/csr.h>
+#include <asm/ptrace.h>
+
+#define EXCEPTION_CAUSE_MAX	16
+
+typedef void (*exception_fn)(struct pt_regs *);
+
+struct thread_info {
+	int cpu;
+	unsigned long hartid;
+	exception_fn exception_handlers[EXCEPTION_CAUSE_MAX];
+};
+
+static inline struct thread_info *current_thread_info(void)
+{
+	return (struct thread_info *)csr_read(CSR_SSCRATCH);
+}
+
+void install_exception_handler(unsigned long cause, void (*handler)(struct pt_regs *));
+void do_handle_exception(struct pt_regs *regs);
+void thread_info_init(void);
+
+#endif /* _ASMRISCV_PROCESSOR_H_ */
lib/riscv/asm/ptrace.h new file +46 -0

46 lines not on this page · raw message

lib/riscv/asm/setup.h +2 -1
index c8cfebb4f2c1..e58dd53071ae 100644
@@ -2,9 +2,10 @@
 #ifndef _ASMRISCV_SETUP_H_
 #define _ASMRISCV_SETUP_H_
 #include <libcflat.h>
+#include <asm/processor.h>
 
 #define NR_CPUS 16
-extern unsigned long cpus[NR_CPUS];       /* per-cpu IDs (hartids) */
+extern struct thread_info cpus[NR_CPUS];
 extern int nr_cpus;
 int hartid_to_cpu(unsigned long hartid);
 
lib/riscv/processor.c new file +60 -0

60 lines not on this page · raw message

lib/riscv/setup.c +6 -3
index 44c26b125a27..57eb4797f798 100644
@@ -12,12 +12,13 @@
 #include <devicetree.h>
 #include <asm/csr.h>
 #include <asm/page.h>
+#include <asm/processor.h>
 #include <asm/setup.h>
 
 char *initrd;
 u32 initrd_size;
 
-unsigned long cpus[NR_CPUS] = { [0 ... NR_CPUS - 1] = ~0UL };
+struct thread_info cpus[NR_CPUS];
 int nr_cpus;
 
 int hartid_to_cpu(unsigned long hartid)
@@ -25,7 +26,7 @@ int hartid_to_cpu(unsigned long hartid)
 	int cpu;
 
 	for_each_present_cpu(cpu)
-		if (cpus[cpu] == hartid)
+		if (cpus[cpu].hartid == hartid)
 			return cpu;
 	return -1;
 }
@@ -36,7 +37,8 @@ static void cpu_set_fdt(int fdtnode __unused, u64 regval, void *info __unused)
 
 	assert_msg(cpu < NR_CPUS, "Number cpus exceeds maximum supported (%d).", NR_CPUS);
 
-	cpus[cpu] = regval;
+	cpus[cpu].cpu = cpu;
+	cpus[cpu].hartid = regval;
 	set_cpu_present(cpu, true);
 }
 
@@ -104,6 +106,7 @@ void setup(const void *fdt, phys_addr_t freemem_start)
 
 	mem_init(PAGE_ALIGN((unsigned long)freemem));
 	cpu_init();
+	thread_info_init();
 	io_init();
 
 	ret = dt_get_bootargs(&bootargs);
riscv/Makefile +1 -0
index fb97e678a456..1243be125c00 100644
@@ -26,6 +26,7 @@ cflatobjs += lib/alloc_phys.o
 cflatobjs += lib/devicetree.o
 cflatobjs += lib/riscv/bitops.o
 cflatobjs += lib/riscv/io.o
+cflatobjs += lib/riscv/processor.o
 cflatobjs += lib/riscv/sbi.o
 cflatobjs += lib/riscv/setup.o
 cflatobjs += lib/riscv/smp.o
riscv/cstart.S +113 -3

131 lines not on this page · raw message

riscv/selftest.c +19 -1
index d3b269cf6255..219093489b62 100644
@@ -6,6 +6,7 @@
  */
 #include <libcflat.h>
 #include <cpumask.h>
+#include <asm/processor.h>
 #include <asm/setup.h>
 
 static void check_cpus(void)
@@ -13,7 +14,23 @@ static void check_cpus(void)
 	int cpu;
 
 	for_each_present_cpu(cpu)
-		report_info("CPU%3d: hartid=%08lx", cpu, cpus[cpu]);
+		report_info("CPU%3d: hartid=%08lx", cpu, cpus[cpu].hartid);
+}
+
+static bool exceptions_work;
+
+static void handler(struct pt_regs *regs)
+{
+	exceptions_work = true;
+	regs->epc += 2;
+}
+
+static void check_exceptions(void)
+{
+	install_exception_handler(EXC_INST_ILLEGAL, handler);
+	asm volatile(".4byte 0");
+	install_exception_handler(EXC_INST_ILLEGAL, NULL);
+	report(exceptions_work, "exceptions");
 }
 
 int main(int argc, char **argv)
@@ -45,6 +62,7 @@ int main(int argc, char **argv)
 		report_skip("environ parsing");
 	}
 
+	check_exceptions();
 	check_cpus();
 
 	return report_summary();
Signature · 1 line
2.43.0
#13 lore raw

PATCH 10/24 riscv: Add backtrace support

In reply to Andrew Jones

Enable stack unwinding, even when going through an exception, by implementing backtrace() and pushing a frame pointer on the stack in exception_vectors.

---
 lib/riscv/asm/stack.h |  3 +++
 lib/riscv/stack.c     | 32 ++++++++++++++++++++++++++++++++
 riscv/Makefile        |  1 +
 riscv/cstart.S        | 28 ++++++++++++++++++++++++++--
 4 files changed, 62 insertions(+), 2 deletions(-)
 create mode 100644 lib/riscv/stack.c
lib/riscv/asm/stack.h +3 -0
index d081d0716d7b..f003ca37c913 100644
@@ -6,4 +6,7 @@
 #error Do not directly include <asm/stack.h>. Just use <stack.h>.
 #endif
 
+#define HAVE_ARCH_BACKTRACE_FRAME
+#define HAVE_ARCH_BACKTRACE
+
 #endif
lib/riscv/stack.c new file +32 -0
new file mode 100644
index 000000000000..712a5478d547
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <libcflat.h>
+#include <stack.h>
+
+int backtrace_frame(const void *frame, const void **return_addrs, int max_depth)
+{
+	static bool walking;
+	const unsigned long *fp = (unsigned long *)frame;
+	int depth;
+
+	if (walking) {
+		printf("RECURSIVE STACK WALK!!!\n");
+		return 0;
+	}
+	walking = true;
+
+	for (depth = 0; fp && depth < max_depth; ++depth) {
+		return_addrs[depth] = (void *)fp[-1];
+		if (return_addrs[depth] == 0)
+			break;
+		fp = (unsigned long *)fp[-2];
+	}
+
+	walking = false;
+	return depth;
+}
+
+int backtrace(const void **return_addrs, int max_depth)
+{
+	return backtrace_frame(__builtin_frame_address(0),
+			       return_addrs, max_depth);
+}
riscv/Makefile +1 -0
index 1243be125c00..4a83f27f7df2 100644
@@ -30,6 +30,7 @@ cflatobjs += lib/riscv/processor.o
 cflatobjs += lib/riscv/sbi.o
 cflatobjs += lib/riscv/setup.o
 cflatobjs += lib/riscv/smp.o
+cflatobjs += lib/riscv/stack.o
 ifeq ($(ARCH),riscv32)
 cflatobjs += lib/ldiv32.o
 endif
riscv/cstart.S +26 -2
index ac7858ef398f..9bdf2e3b17dd 100644
@@ -17,6 +17,22 @@
 
 #define REG_L	__REG_SEL(ld, lw)
 #define REG_S	__REG_SEL(sd, sw)
+#define SZREG	__REG_SEL(8, 4)
+
+#define FP_SIZE 16
+
+.macro push_fp, ra=ra
+	addi	sp, sp, -FP_SIZE
+	REG_S	\ra, (FP_SIZE - SZREG)(sp)
+	REG_S	fp, (FP_SIZE - 2*SZREG)(sp)
+	addi	fp, sp, FP_SIZE
+.endm
+
+.macro pop_fp
+	REG_L	ra, (FP_SIZE - SZREG)(sp)
+	REG_L	fp, (FP_SIZE - 2*SZREG)(sp)
+	addi	sp, sp, FP_SIZE
+.endm
 
 .macro zero_range, tmp1, tmp2
 9998:	beq	\tmp1, \tmp2, 9997f
@@ -73,6 +89,7 @@ start:
 	li	a1, -8192
 	add	a1, sp, a1
 	zero_range a1, sp
+	mv	fp, zero			// Ensure fp starts out as zero
 
 	/* set up exception handling */
 	la	a1, exception_vectors
@@ -200,9 +217,16 @@ halt:
 .balign 4
 .global exception_vectors
 exception_vectors:
-	REG_S	a0, (-PT_SIZE + PT_ORIG_A0)(sp)
-	addi	a0, sp, -PT_SIZE
+	REG_S	a0, (-PT_SIZE - FP_SIZE + PT_ORIG_A0)(sp)
+	addi	a0, sp, -PT_SIZE - FP_SIZE
 	save_context
+	/*
+	 * Set a frame pointer "ra" which points to the last instruction.
+	 * Add 1 to it, because pretty_print_stacks.py subtracts 1.
+	 */
+	REG_L	a1, PT_EPC(a0)
+	addi	a1, a1, 1
+	push_fp	a1
 	mv	sp, a0
 	call	do_handle_exception
 	restore_context
Signature · 1 line
2.43.0
#14 lore raw

PATCH 11/24 arm/arm64: Generalize wfe/sev names in smp.c

In reply to Andrew Jones

Most of Arm's on_cpus() implementation can be shared by any architecture which has the possible, present, and idle cpumasks, like riscv does. Rename the exceptions (wfe/sve) to something more generic in order to prepare to share the functions.

---
 lib/arm/asm/smp.h |  4 ++++
 lib/arm/smp.c     | 16 ++++++++--------
 2 files changed, 12 insertions(+), 8 deletions(-)
lib/arm/asm/smp.h +4 -0
index b89a68dd344f..9f6d839ab568 100644
@@ -6,6 +6,7 @@
  * This work is licensed under the terms of the GNU LGPL, version 2.
  */
 #include <cpumask.h>
+#include <asm/barrier.h>
 #include <asm/thread_info.h>
 
 #define smp_processor_id()		(current_thread_info()->cpu)
@@ -18,6 +19,9 @@ struct secondary_data {
 };
 extern struct secondary_data secondary_data;
 
+#define smp_wait_for_event()	wfe()
+#define smp_send_event()	sev()
+
 extern bool cpu0_calls_idle;
 
 extern void halt(void);
lib/arm/smp.c +8 -8
index 78fc1656cefa..c00fda2efb03 100644
@@ -45,7 +45,7 @@ secondary_entry_fn secondary_cinit(void)
 	 */
 	entry = secondary_data.entry;
 	set_cpu_online(ti->cpu, true);
-	sev();
+	smp_send_event();
 
 	/*
 	 * Return to the assembly stub, allowing entry to be called
@@ -65,7 +65,7 @@ static void __smp_boot_secondary(int cpu, secondary_entry_fn entry)
 	assert(ret == 0);
 
 	while (!cpu_online(cpu))
-		wfe();
+		smp_wait_for_event();
 }
 
 void smp_boot_secondary(int cpu, secondary_entry_fn entry)
@@ -122,7 +122,7 @@ static void cpu_wait(int cpu)
 	cpumask_set_cpu(me, &on_cpu_info[cpu].waiters);
 	deadlock_check(me, cpu);
 	while (!cpu_idle(cpu))
-		wfe();
+		smp_wait_for_event();
 	cpumask_clear_cpu(me, &on_cpu_info[cpu].waiters);
 }
 
@@ -134,17 +134,17 @@ void do_idle(void)
 		cpu0_calls_idle = true;
 
 	set_cpu_idle(cpu, true);
-	sev();
+	smp_send_event();
 
 	for (;;) {
 		while (cpu_idle(cpu))
-			wfe();
+			smp_wait_for_event();
 		smp_rmb();
 		on_cpu_info[cpu].func(on_cpu_info[cpu].data);
 		on_cpu_info[cpu].func = NULL;
 		smp_wmb();
 		set_cpu_idle(cpu, true);
-		sev();
+		smp_send_event();
 	}
 }
 
@@ -174,7 +174,7 @@ void on_cpu_async(int cpu, void (*func)(void *data), void *data)
 	on_cpu_info[cpu].data = data;
 	spin_unlock(&lock);
 	set_cpu_idle(cpu, false);
-	sev();
+	smp_send_event();
 }
 
 void on_cpu(int cpu, void (*func)(void *data), void *data)
@@ -201,7 +201,7 @@ void on_cpus(void (*func)(void *data), void *data)
 		deadlock_check(me, cpu);
 	}
 	while (cpumask_weight(&cpu_idle_mask) < nr_cpus - 1)
-		wfe();
+		smp_wait_for_event();
 	for_each_present_cpu(cpu)
 		cpumask_clear_cpu(me, &on_cpu_info[cpu].waiters);
 }
Signature · 1 line
2.43.0
#15 lore raw

PATCH 12/24 arm/arm64: Remove spinlocks from on_cpu_async

In reply to Andrew Jones

Remove spinlocks from on_cpu_async() by pulling some of their use into a new function and also by narrowing the locking to a single on_cpu_info structure by introducing yet another cpumask.

---
 lib/arm/asm/smp.h |  4 +++-
 lib/arm/smp.c     | 37 ++++++++++++++++++++++++++++---------
 2 files changed, 31 insertions(+), 10 deletions(-)
lib/arm/asm/smp.h +3 -1
index 9f6d839ab568..f0c0f97a19f8 100644
@@ -27,9 +27,11 @@ extern bool cpu0_calls_idle;
 extern void halt(void);
 extern void do_idle(void);
 
-extern void smp_boot_secondary(int cpu, secondary_entry_fn entry);
 extern void on_cpu_async(int cpu, void (*func)(void *data), void *data);
 extern void on_cpu(int cpu, void (*func)(void *data), void *data);
 extern void on_cpus(void (*func)(void *data), void *data);
 
+extern void smp_boot_secondary(int cpu, secondary_entry_fn entry);
+extern void smp_boot_secondary_nofail(int cpu, secondary_entry_fn entry);
+
 #endif /* _ASMARM_SMP_H_ */
lib/arm/smp.c +28 -9
index c00fda2efb03..e0872a1a72c2 100644
@@ -76,12 +76,32 @@ void smp_boot_secondary(int cpu, secondary_entry_fn entry)
 	spin_unlock(&lock);
 }
 
+void smp_boot_secondary_nofail(int cpu, secondary_entry_fn entry)
+{
+	spin_lock(&lock);
+	if (!cpu_online(cpu))
+		__smp_boot_secondary(cpu, entry);
+	spin_unlock(&lock);
+}
+
 struct on_cpu_info {
 	void (*func)(void *data);
 	void *data;
 	cpumask_t waiters;
 };
 static struct on_cpu_info on_cpu_info[NR_CPUS];
+static cpumask_t on_cpu_info_lock;
+
+static bool get_on_cpu_info(int cpu)
+{
+	return !cpumask_test_and_set_cpu(cpu, &on_cpu_info_lock);
+}
+
+static void put_on_cpu_info(int cpu)
+{
+	int ret = cpumask_test_and_clear_cpu(cpu, &on_cpu_info_lock);
+	assert(ret);
+}
 
 static void __deadlock_check(int cpu, const cpumask_t *waiters, bool *found)
 {
@@ -158,22 +178,21 @@ void on_cpu_async(int cpu, void (*func)(void *data), void *data)
 	assert_msg(cpu != 0 || cpu0_calls_idle, "Waiting on CPU0, which is unlikely to idle. "
 						"If this is intended set cpu0_calls_idle=1");
 
-	spin_lock(&lock);
-	if (!cpu_online(cpu))
-		__smp_boot_secondary(cpu, do_idle);
-	spin_unlock(&lock);
+	smp_boot_secondary_nofail(cpu, do_idle);
 
 	for (;;) {
 		cpu_wait(cpu);
-		spin_lock(&lock);
-		if ((volatile void *)on_cpu_info[cpu].func == NULL)
-			break;
-		spin_unlock(&lock);
+		if (get_on_cpu_info(cpu)) {
+			if ((volatile void *)on_cpu_info[cpu].func == NULL)
+				break;
+			put_on_cpu_info(cpu);
+		}
 	}
+
 	on_cpu_info[cpu].func = func;
 	on_cpu_info[cpu].data = data;
-	spin_unlock(&lock);
 	set_cpu_idle(cpu, false);
+	put_on_cpu_info(cpu);
 	smp_send_event();
 }
 
Signature · 1 line
2.43.0
#16 lore raw

PATCH 13/24 arm/arm64: Share on_cpus

In reply to Andrew Jones

Now that the previous patches have cleaned up Arm's on_cpus implementation we can move it to the common lib where riscv will share it.

---
 arm/Makefile.common |   1 +
 lib/arm/asm/smp.h   |   8 +--
 lib/arm/smp.c       | 144 -----------------------------------------
 lib/on-cpus.c       | 154 ++++++++++++++++++++++++++++++++++++++++++++
 lib/on-cpus.h       |  14 ++++
 5 files changed, 170 insertions(+), 151 deletions(-)
 create mode 100644 lib/on-cpus.c
 create mode 100644 lib/on-cpus.h
arm/Makefile.common +1 -0
index 5214c8acdab3..dc92a7433350 100644
@@ -43,6 +43,7 @@ cflatobjs += lib/vmalloc.o
 cflatobjs += lib/alloc.o
 cflatobjs += lib/devicetree.o
 cflatobjs += lib/migrate.o
+cflatobjs += lib/on-cpus.o
 cflatobjs += lib/pci.o
 cflatobjs += lib/pci-host-generic.o
 cflatobjs += lib/pci-testdev.o
lib/arm/asm/smp.h +1 -7
index f0c0f97a19f8..2e1dc27f7bd8 100644
@@ -6,6 +6,7 @@
  * This work is licensed under the terms of the GNU LGPL, version 2.
  */
 #include <cpumask.h>
+#include <on-cpus.h>
 #include <asm/barrier.h>
 #include <asm/thread_info.h>
 
@@ -22,14 +23,7 @@ extern struct secondary_data secondary_data;
 #define smp_wait_for_event()	wfe()
 #define smp_send_event()	sev()
 
-extern bool cpu0_calls_idle;
-
 extern void halt(void);
-extern void do_idle(void);
-
-extern void on_cpu_async(int cpu, void (*func)(void *data), void *data);
-extern void on_cpu(int cpu, void (*func)(void *data), void *data);
-extern void on_cpus(void (*func)(void *data), void *data);
 
 extern void smp_boot_secondary(int cpu, secondary_entry_fn entry);
 extern void smp_boot_secondary_nofail(int cpu, secondary_entry_fn entry);
lib/arm/smp.c +0 -144

157 lines not on this page · raw message

lib/on-cpus.c new file +154 -0

154 lines not on this page · raw message

lib/on-cpus.h new file +14 -0
new file mode 100644
index 000000000000..41103b0245c7
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ON_CPUS_H_
+#define _ON_CPUS_H_
+#include <stdbool.h>
+
+extern bool cpu0_calls_idle;
+
+void do_idle(void);
+
+void on_cpu_async(int cpu, void (*func)(void *data), void *data);
+void on_cpu(int cpu, void (*func)(void *data), void *data);
+void on_cpus(void (*func)(void *data), void *data);
+
+#endif /* _ON_CPUS_H_ */
Signature · 1 line
2.43.0
#17 lore raw

PATCH 14/24 riscv: Compile with march

In reply to Andrew Jones

Tell the compiler to provide mnemonics for instructions we depend on, such as 'pause'. Specifying march also allows extensions which affect compilation to be individually [un]selected. For example, building without compressed (2 byte) instructions may be desirable, so 'c' may be removed from the march isa string.

---
 riscv/Makefile | 5 +++++
 1 file changed, 5 insertions(+)
riscv/Makefile +5 -0
index 4a83f27f7df2..697a3beb2703 100644
@@ -55,8 +55,13 @@ define arch_elf_check =
 		$(error $(1) has unsupported reloc types))
 endef
 
+ISA_COMMON = mafdc_zicsr_zifencei_zihintpause
+
 ifeq ($(ARCH),riscv64)
+CFLAGS += -march=rv64i$(ISA_COMMON)
 CFLAGS += -DCONFIG_64BIT
+else ifeq ($(ARCH),riscv32)
+CFLAGS += -march=rv32i$(ISA_COMMON)
 endif
 CFLAGS += -DCONFIG_RELOC
 CFLAGS += -mcmodel=medany
Signature · 1 line
2.43.0
#18 lore raw

PATCH 15/24 riscv: Add SMP support

In reply to Andrew Jones

Implement the same SMP API that Arm has but using an SBI HSM call instead of PSCI. Unlike Arm, riscv needs to always set cpu0_calls_idle, because the boot hart doesn't have to be the first hart described in the DT, which means cpu0 may well be a secondary. As usual, add a couple tests to selftest.c to make sure everything works.

(The secondary boot process is also improved over Arm's a bit by keeping boot data percpu, dropping the need for a lock. We could create percpu data for Arm too, but that's left as future work.)

---
 lib/riscv/asm-offsets.c |  6 ++++
 lib/riscv/asm/barrier.h |  2 ++
 lib/riscv/asm/sbi.h     | 22 +++++++++++++++
 lib/riscv/asm/smp.h     | 28 +++++++++++++++++++
 lib/riscv/processor.c   |  2 ++
 lib/riscv/sbi.c         |  5 ++++
 lib/riscv/setup.c       |  2 ++
 lib/riscv/smp.c         | 61 +++++++++++++++++++++++++++++++++++++++++
 riscv/Makefile          |  1 +
 riscv/cstart.S          | 25 +++++++++++++++++
 riscv/selftest.c        | 31 +++++++++++++++++++++
 11 files changed, 185 insertions(+)
 create mode 100644 lib/riscv/asm/smp.h
lib/riscv/asm-offsets.c +6 -0
index 7b88d16fd0e4..f5beeeb45e09 100644
@@ -2,6 +2,7 @@
 #include <kbuild.h>
 #include <elf.h>
 #include <asm/ptrace.h>
+#include <asm/smp.h>
 
 int main(void)
 {
@@ -51,5 +52,10 @@ int main(void)
 	OFFSET(PT_CAUSE, pt_regs, cause);
 	OFFSET(PT_ORIG_A0, pt_regs, orig_a0);
 	DEFINE(PT_SIZE, sizeof(struct pt_regs));
+
+	OFFSET(SECONDARY_STVEC, secondary_data, stvec);
+	OFFSET(SECONDARY_FUNC, secondary_data, func);
+	DEFINE(SECONDARY_DATA_SIZE, sizeof(struct secondary_data));
+
 	return 0;
 }
lib/riscv/asm/barrier.h +2 -0
index 6036d66af76f..4fef120a0fe8 100644
@@ -15,4 +15,6 @@
 #define smp_rmb()	RISCV_FENCE(r,r)
 #define smp_wmb()	RISCV_FENCE(w,w)
 
+#define cpu_relax()	__asm__ __volatile__ ("pause")
+
 #endif /* _ASMRISCV_BARRIER_H_ */
lib/riscv/asm/sbi.h +22 -0
index aeff07f6f1a8..d82a384da5ce 100644
@@ -2,8 +2,21 @@
 #ifndef _ASMRISCV_SBI_H_
 #define _ASMRISCV_SBI_H_
 
+#define SBI_SUCCESS			0
+#define SBI_ERR_FAILURE			-1
+#define SBI_ERR_NOT_SUPPORTED		-2
+#define SBI_ERR_INVALID_PARAM		-3
+#define SBI_ERR_DENIED			-4
+#define SBI_ERR_INVALID_ADDRESS		-5
+#define SBI_ERR_ALREADY_AVAILABLE	-6
+#define SBI_ERR_ALREADY_STARTED		-7
+#define SBI_ERR_ALREADY_STOPPED		-8
+
+#ifndef __ASSEMBLY__
+
 enum sbi_ext_id {
 	SBI_EXT_BASE = 0x10,
+	SBI_EXT_HSM = 0x48534d,
 	SBI_EXT_SRST = 0x53525354,
 };
 
@@ -17,6 +30,13 @@ enum sbi_ext_base_fid {
 	SBI_EXT_BASE_GET_MIMPID,
 };
 
+enum sbi_ext_hsm_fid {
+	SBI_EXT_HSM_HART_START = 0,
+	SBI_EXT_HSM_HART_STOP,
+	SBI_EXT_HSM_HART_STATUS,
+	SBI_EXT_HSM_HART_SUSPEND,
+};
+
 struct sbiret {
 	long error;
 	long value;
@@ -28,5 +48,7 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
 			unsigned long arg5);
 
 void sbi_shutdown(void);
+struct sbiret sbi_hart_start(unsigned long hartid, unsigned long entry, unsigned long sp);
 
+#endif /* !__ASSEMBLY__ */
 #endif /* _ASMRISCV_SBI_H_ */
lib/riscv/asm/smp.h new file +28 -0
new file mode 100644
index 000000000000..931766dc3969
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASMRISCV_SMP_H_
+#define _ASMRISCV_SMP_H_
+#include <asm/barrier.h>
+#include <asm/processor.h>
+
+#define smp_wait_for_event()	cpu_relax()
+#define smp_send_event()	cpu_relax()
+
+static inline int smp_processor_id(void)
+{
+	return current_thread_info()->cpu;
+}
+
+typedef void (*secondary_func_t)(void);
+
+struct secondary_data {
+	unsigned long stvec;
+	secondary_func_t func;
+} __attribute__((aligned(16)));
+
+void secondary_entry(unsigned long hartid, unsigned long sp_phys);
+secondary_func_t secondary_cinit(struct secondary_data *data);
+
+void smp_boot_secondary(int cpu, void (*func)(void));
+void smp_boot_secondary_nofail(int cpu, void (*func)(void));
+
+#endif /* _ASMRISCV_SMP_H_ */
lib/riscv/processor.c +2 -0
index 3790349aadd1..7248cf4c5ca6 100644
@@ -11,10 +11,12 @@ extern unsigned long _text;
 
 static void show_regs(struct pt_regs *regs)
 {
+	struct thread_info *info = current_thread_info();
 	uintptr_t text = (uintptr_t)&_text;
 	unsigned int w = __riscv_xlen / 4;
 
 	printf("Load address: %" PRIxPTR "\n", text);
+	printf("CPU%3d : hartid=%lx\n", info->cpu, info->hartid);
 	printf("status : %.*lx\n", w, regs->status);
 	printf("cause  : %.*lx\n", w, regs->cause);
 	printf("badaddr: %.*lx\n", w, regs->badaddr);
lib/riscv/sbi.c +5 -0
index fd758555b888..f39134c4d77e 100644
@@ -33,3 +33,8 @@ void sbi_shutdown(void)
 	sbi_ecall(SBI_EXT_SRST, 0, 0, 0, 0, 0, 0, 0);
 	puts("SBI shutdown failed!\n");
 }
+
+struct sbiret sbi_hart_start(unsigned long hartid, unsigned long entry, unsigned long sp)
+{
+	return sbi_ecall(SBI_EXT_HSM, SBI_EXT_HSM_HART_START, hartid, entry, sp, 0, 0, 0);
+}
lib/riscv/setup.c +2 -0
index 57eb4797f798..9ff446b5e171 100644
@@ -10,6 +10,7 @@
 #include <argv.h>
 #include <cpumask.h>
 #include <devicetree.h>
+#include <on-cpus.h>
 #include <asm/csr.h>
 #include <asm/page.h>
 #include <asm/processor.h>
@@ -60,6 +61,7 @@ static void cpu_init(void)
 	}
 
 	set_cpu_online(hartid_to_cpu(csr_read(CSR_SSCRATCH)), true);
+	cpu0_calls_idle = true;
 }
 
 static void mem_init(phys_addr_t freemem_start)
lib/riscv/smp.c +61 -0

67 lines not on this page · raw message

riscv/Makefile +1 -0
index 697a3beb2703..932f3378264c 100644
@@ -24,6 +24,7 @@ cstart.o = $(TEST_DIR)/cstart.o
 cflatobjs += lib/alloc.o
 cflatobjs += lib/alloc_phys.o
 cflatobjs += lib/devicetree.o
+cflatobjs += lib/on-cpus.o
 cflatobjs += lib/riscv/bitops.o
 cflatobjs += lib/riscv/io.o
 cflatobjs += lib/riscv/processor.o
riscv/cstart.S +25 -0
index 9bdf2e3b17dd..145ccaab6159 100644
@@ -117,6 +117,31 @@ halt:
 1:	wfi
 	j	1b
 
+.balign 4
+.global secondary_entry
+secondary_entry:
+	/*
+	 * From the "HSM Hart Start Register State" table of the SBI spec:
+	 *	satp		0
+	 *	sstatus.SIE	0
+	 *	a0		hartid
+	 *	a1		opaque parameter
+	 *
+	 * __smp_boot_secondary() sets the opaque parameter (a1) to the physical
+	 * address of the stack and the stack contains the secondary data.
+	 */
+	csrw	CSR_SSCRATCH, a0
+	mv	sp, a1
+	mv	fp, zero
+	REG_L	a0, SECONDARY_STVEC(sp)
+	csrw	CSR_STVEC, a0
+	mv	a0, sp
+	call	secondary_cinit
+	addi	sp, sp, SECONDARY_DATA_SIZE
+	jr	a0
+	la	a0, do_idle
+	jr	a0
+
 /*
  * Save context to address in a0.
  * For a0, sets PT_A0(a0) to the contents of PT_ORIG_A0(a0).
riscv/selftest.c +31 -0
index 219093489b62..da13c622dba7 100644
@@ -6,8 +6,10 @@
  */
 #include <libcflat.h>
 #include <cpumask.h>
+#include <on-cpus.h>
 #include <asm/processor.h>
 #include <asm/setup.h>
+#include <asm/smp.h>
 
 static void check_cpus(void)
 {
@@ -33,6 +35,34 @@ static void check_exceptions(void)
 	report(exceptions_work, "exceptions");
 }
 
+static cpumask_t cpus_alive;
+
+static void check_secondary(void *data)
+{
+	cpumask_set_cpu(smp_processor_id(), &cpus_alive);
+}
+
+static void check_smp(void)
+{
+	int cpu, me = smp_processor_id();
+	bool fail = false;
+
+	on_cpus(check_secondary, NULL);
+
+	report(cpumask_full(&cpu_online_mask), "Brought up all cpus");
+	report(cpumask_full(&cpus_alive), "check_secondary");
+
+	for_each_present_cpu(cpu) {
+		if (cpu == me)
+			continue;
+		if (!cpu_idle(cpu)) {
+			fail = true;
+			break;
+		}
+	}
+	report(!fail, "All secondaries are idle");
+}
+
 int main(int argc, char **argv)
 {
 	bool r;
@@ -64,6 +94,7 @@ int main(int argc, char **argv)
 
 	check_exceptions();
 	check_cpus();
+	check_smp();
 
 	return report_summary();
 }
Signature · 1 line
2.43.0
#19 lore raw

PATCH 16/24 arm/arm64: Share memregions

In reply to Andrew Jones

arm/arm64 (and to a small extent powerpc) have memory regions which get built from hardware descriptions (DT/ACPI/EFI) and then used to build page tables. Move memregions to common code, tweaking the API a bit at the same time, e.g. change 'mem_region' to 'memregions'. The biggest change is there is now a default number of memory regions which, if too small, should be overridden at setup time with a new init function, memregions_init().

---
 arm/Makefile.common |  1 +
 arm/selftest.c      |  3 +-
 lib/arm/asm/setup.h | 14 -------
 lib/arm/mmu.c       |  1 +
 lib/arm/setup.c     | 93 ++++++++++-----------------------------------
 lib/memregions.c    | 82 +++++++++++++++++++++++++++++++++++++++
 lib/memregions.h    | 29 ++++++++++++++
 7 files changed, 136 insertions(+), 87 deletions(-)
 create mode 100644 lib/memregions.c
 create mode 100644 lib/memregions.h
arm/Makefile.common +1 -0
index dc92a7433350..4dfd570fa59e 100644
@@ -42,6 +42,7 @@ cflatobjs += lib/alloc_page.o
 cflatobjs += lib/vmalloc.o
 cflatobjs += lib/alloc.o
 cflatobjs += lib/devicetree.o
+cflatobjs += lib/memregions.o
 cflatobjs += lib/migrate.o
 cflatobjs += lib/on-cpus.o
 cflatobjs += lib/pci.o
arm/selftest.c +2 -1
index 9f459ed3d571..007d2309d01c 100644
@@ -8,6 +8,7 @@
 #include <libcflat.h>
 #include <util.h>
 #include <devicetree.h>
+#include <memregions.h>
 #include <vmalloc.h>
 #include <asm/setup.h>
 #include <asm/ptrace.h>
@@ -90,7 +91,7 @@ static bool check_pabt_init(void)
 			highest_end = PAGE_ALIGN(r->end);
 	}
 
-	if (mem_region_get_flags(highest_end) != MR_F_UNKNOWN)
+	if (memregions_get_flags(highest_end) != MR_F_UNKNOWN)
 		return false;
 
 	vaddr = (unsigned long)vmap(highest_end, PAGE_SIZE);
lib/arm/asm/setup.h +0 -14
index 060691165a20..9f8ef82efb90 100644
@@ -13,22 +13,8 @@
 extern u64 cpus[NR_CPUS];	/* per-cpu IDs (MPIDRs) */
 extern int nr_cpus;
 
-#define MR_F_IO			(1U << 0)
-#define MR_F_CODE		(1U << 1)
-#define MR_F_RESERVED		(1U << 2)
-#define MR_F_UNKNOWN		(1U << 31)
-
-struct mem_region {
-	phys_addr_t start;
-	phys_addr_t end;
-	unsigned int flags;
-};
-extern struct mem_region *mem_regions;
 extern phys_addr_t __phys_offset, __phys_end;
 
-extern struct mem_region *mem_region_find(phys_addr_t paddr);
-extern unsigned int mem_region_get_flags(phys_addr_t paddr);
-
 #define PHYS_OFFSET		(__phys_offset)
 #define PHYS_END		(__phys_end)
 
lib/arm/mmu.c +1 -0
index b16517a3200d..eb5e82a95f06 100644
@@ -6,6 +6,7 @@
  * This work is licensed under the terms of the GNU LGPL, version 2.
  */
 #include <cpumask.h>
+#include <memregions.h>
 #include <asm/setup.h>
 #include <asm/thread_info.h>
 #include <asm/mmu.h>
lib/arm/setup.c +21 -72

164 lines not on this page · raw message

lib/memregions.c new file +82 -0

82 lines not on this page · raw message

lib/memregions.h new file +29 -0
new file mode 100644
index 000000000000..9a8e33182fe5
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _MEMREGIONS_H_
+#define _MEMREGIONS_H_
+#include <libcflat.h>
+#include <bitops.h>
+
+#define NR_INITIAL_MEM_REGIONS		8
+
+#define MR_F_IO				BIT(0)
+#define MR_F_CODE			BIT(1)
+#define MR_F_RESERVED			BIT(2)
+#define MR_F_UNKNOWN			BIT(31)
+
+struct mem_region {
+	phys_addr_t start;
+	phys_addr_t end;
+	uint32_t flags;
+};
+
+extern struct mem_region *mem_regions;
+
+void memregions_init(struct mem_region regions[], size_t nr);
+struct mem_region *memregions_add(struct mem_region *r);
+struct mem_region *memregions_find(phys_addr_t paddr);
+uint32_t memregions_get_flags(phys_addr_t paddr);
+void memregions_split(phys_addr_t addr, struct mem_region **r1, struct mem_region **r2);
+void memregions_add_dt_regions(size_t max_nr);
+
+#endif /* _MEMREGIONS_H_ */
Signature · 1 line
2.43.0
#20 lore raw

PATCH 17/24 riscv: Populate memregions and switch to page allocator

In reply to Andrew Jones

Get the memory regions from the DT rather than just assuming we have enough. Getting memory regions and setting their flags is also a first step to enabling the MMU. Also switch to the page allocator, which is a second step to enabling the MMU.

---
 lib/riscv/asm/memory_areas.h |  1 +
 lib/riscv/setup.c            | 55 ++++++++++++++++++++++++++++++++++--
 riscv/Makefile               |  2 ++
 3 files changed, 55 insertions(+), 3 deletions(-)
 create mode 100644 lib/riscv/asm/memory_areas.h
lib/riscv/asm/memory_areas.h new file +1 -0
new file mode 100644
index 000000000000..2b34e63502dd
@@ -0,0 +1 @@
+#include <asm-generic/memory_areas.h>
lib/riscv/setup.c +52 -3

91 lines not on this page · raw message

riscv/Makefile +2 -0
index 932f3378264c..ed1a14025ed2 100644
@@ -22,8 +22,10 @@ $(TEST_DIR)/sieve.elf: AUXFLAGS = 0x1
 cstart.o = $(TEST_DIR)/cstart.o
 
 cflatobjs += lib/alloc.o
+cflatobjs += lib/alloc_page.o
 cflatobjs += lib/alloc_phys.o
 cflatobjs += lib/devicetree.o
+cflatobjs += lib/memregions.o
 cflatobjs += lib/on-cpus.o
 cflatobjs += lib/riscv/bitops.o
 cflatobjs += lib/riscv/io.o
Signature · 1 line
2.43.0
#21 lore raw

PATCH 18/24 riscv: Add MMU support

In reply to Andrew Jones

Add minimal page table defines and functions in order to build page tables and enable the MMU.

---
 lib/riscv/asm/csr.h     |   1 +
 lib/riscv/asm/io.h      |   3 +
 lib/riscv/asm/mmu.h     |  28 ++++++++
 lib/riscv/asm/page.h    |  11 ++++
 lib/riscv/asm/pgtable.h |  42 ++++++++++++
 lib/riscv/mmu.c         | 140 ++++++++++++++++++++++++++++++++++++++++
 lib/riscv/setup.c       |   3 +
 riscv/Makefile          |   1 +
 8 files changed, 229 insertions(+)
 create mode 100644 lib/riscv/asm/mmu.h
 create mode 100644 lib/riscv/asm/pgtable.h
 create mode 100644 lib/riscv/mmu.c
lib/riscv/asm/csr.h +1 -0
index eeddd1fb448a..88c816c68d09 100644
@@ -9,6 +9,7 @@
 #define CSR_SEPC		0x141
 #define CSR_SCAUSE		0x142
 #define CSR_STVAL		0x143
+#define CSR_SATP		0x180
 
 /* Exception cause high bit - is an interrupt if set */
 #define CAUSE_IRQ_FLAG		(_AC(1, UL) << (__riscv_xlen - 1))
lib/riscv/asm/io.h +3 -0
index d2eb3acc9fda..6fe111289102 100644
@@ -73,6 +73,9 @@ static inline u64 __raw_readq(const volatile void __iomem *addr)
 }
 #endif
 
+#define ioremap ioremap
+void __iomem *ioremap(phys_addr_t phys_addr, size_t size);
+
 #include <asm-generic/io.h>
 
 #endif /* _ASMRISCV_IO_H_ */
lib/riscv/asm/mmu.h new file +28 -0
new file mode 100644
index 000000000000..02703f607511
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASMRISCV_MMU_H_
+#define _ASMRISCV_MMU_H_
+#include <libcflat.h>
+#include <asm/csr.h>
+#include <asm/page.h>
+#include <asm/pgtable.h>
+
+static inline pgd_t *current_pgtable(void)
+{
+	return (pgd_t *)((csr_read(CSR_SATP) & SATP_PPN) << PAGE_SHIFT);
+}
+
+void mmu_set_range_ptes(pgd_t *pgtable, uintptr_t virt_offset,
+			phys_addr_t phys_start, phys_addr_t phys_end,
+			pgprot_t prot, bool flush);
+void __mmu_enable(unsigned long satp);
+void mmu_enable(unsigned long mode, pgd_t *pgtable);
+void mmu_disable(void);
+
+void setup_mmu(void);
+
+static inline void local_flush_tlb_page(unsigned long addr)
+{
+	asm volatile("sfence.vma %0" : : "r" (addr) : "memory");
+}
+
+#endif /* _ASMRISCV_MMU_H_ */
lib/riscv/asm/page.h +11 -0
index 7d7c9191605a..9801b4d1b9c1 100644
@@ -2,6 +2,17 @@
 #ifndef _ASMRISCV_PAGE_H_
 #define _ASMRISCV_PAGE_H_
 
+#ifndef __ASSEMBLY__
+
+typedef unsigned long pgd_t;
+typedef unsigned long pte_t;
+typedef unsigned long pgprot_t;
+typedef unsigned long pteval_t;
+
+#define __pgprot(x)		((pgprot_t)(x))
+
+#endif /* !__ASSEMBLY__ */
+
 #include <asm-generic/page.h>
 
 #endif /* _ASMRISCV_PAGE_H_ */
lib/riscv/asm/pgtable.h new file +42 -0

42 lines not on this page · raw message

lib/riscv/mmu.c new file +140 -0

140 lines not on this page · raw message

lib/riscv/setup.c +3 -0
index 848ec8e83496..c4c1bd58b337 100644
@@ -14,6 +14,7 @@
 #include <memregions.h>
 #include <on-cpus.h>
 #include <asm/csr.h>
+#include <asm/mmu.h>
 #include <asm/page.h>
 #include <asm/processor.h>
 #include <asm/setup.h>
@@ -171,5 +172,7 @@ void setup(const void *fdt, phys_addr_t freemem_start)
 		setup_env(env, initrd_size);
 	}
 
+	setup_mmu();
+
 	banner();
 }
riscv/Makefile +1 -0
index ed1a14025ed2..821891b719e7 100644
@@ -29,6 +29,7 @@ cflatobjs += lib/memregions.o
 cflatobjs += lib/on-cpus.o
 cflatobjs += lib/riscv/bitops.o
 cflatobjs += lib/riscv/io.o
+cflatobjs += lib/riscv/mmu.o
 cflatobjs += lib/riscv/processor.o
 cflatobjs += lib/riscv/sbi.o
 cflatobjs += lib/riscv/setup.o
Signature · 1 line
2.43.0
#22 lore raw

PATCH 19/24 riscv: Enable the MMU in secondaries

In reply to Andrew Jones

Set each secondary satp to the same as the primary's and enable the MMU when starting. We also change the memalign() to alloc_pages() to prepare for enabling vmalloc_ops. We always want an address for the stack where its virtual address is the same as its physical address, but vmalloc_ops.memalign wouldn't provide that.

---
 lib/riscv/asm-offsets.c | 1 +
 lib/riscv/asm/smp.h     | 1 +
 lib/riscv/smp.c         | 7 +++++--
 3 files changed, 7 insertions(+), 2 deletions(-)
lib/riscv/asm-offsets.c +1 -0
index f5beeeb45e09..a2a32438e075 100644
@@ -53,6 +53,7 @@ int main(void)
 	OFFSET(PT_ORIG_A0, pt_regs, orig_a0);
 	DEFINE(PT_SIZE, sizeof(struct pt_regs));
 
+	OFFSET(SECONDARY_SATP, secondary_data, satp);
 	OFFSET(SECONDARY_STVEC, secondary_data, stvec);
 	OFFSET(SECONDARY_FUNC, secondary_data, func);
 	DEFINE(SECONDARY_DATA_SIZE, sizeof(struct secondary_data));
lib/riscv/asm/smp.h +1 -0
index 931766dc3969..b3ead4e86433 100644
@@ -15,6 +15,7 @@ static inline int smp_processor_id(void)
 typedef void (*secondary_func_t)(void);
 
 struct secondary_data {
+	unsigned long satp;
 	unsigned long stvec;
 	secondary_func_t func;
 } __attribute__((aligned(16)));
lib/riscv/smp.c +5 -2
index ed7984e75608..7e4bb5b76903 100644
@@ -5,9 +5,10 @@
  * Copyright (C) 2023, Ventana Micro Systems Inc., Andrew Jones <[email protected]>
  */
 #include <libcflat.h>
-#include <alloc.h>
+#include <alloc_page.h>
 #include <cpumask.h>
 #include <asm/csr.h>
+#include <asm/mmu.h>
 #include <asm/page.h>
 #include <asm/processor.h>
 #include <asm/sbi.h>
@@ -23,6 +24,7 @@ secondary_func_t secondary_cinit(struct secondary_data *data)
 {
 	struct thread_info *info;
 
+	__mmu_enable(data->satp);
 	thread_info_init();
 	info = current_thread_info();
 	set_cpu_online(info->cpu, true);
@@ -33,10 +35,11 @@ secondary_func_t secondary_cinit(struct secondary_data *data)
 
 static void __smp_boot_secondary(int cpu, secondary_func_t func)
 {
-	struct secondary_data *sp = memalign(16, SZ_8K) + SZ_8K - 16;
+	struct secondary_data *sp = alloc_pages(1) + SZ_8K - 16;
 	struct sbiret ret;
 
 	sp -= sizeof(struct secondary_data);
+	sp->satp = csr_read(CSR_SATP);
 	sp->stvec = csr_read(CSR_STVEC);
 	sp->func = func;
 
Signature · 1 line
2.43.0
#23 lore raw

PATCH 20/24 riscv: Enable vmalloc

In reply to Andrew Jones

Implement the functions that vmalloc depends on and let it enable the MMU through setup_vm(). We can now also run the sieve test, so we add it as well.

---
 lib/riscv/asm/io.h  |  6 +++++
 lib/riscv/asm/mmu.h |  2 --
 lib/riscv/mmu.c     | 57 ++++++++++++++++++++++++++++++++++++++++++++-
 lib/riscv/setup.c   | 16 ++++++++++---
 riscv/Makefile      |  3 ++-
 riscv/sieve.c       |  1 +
 6 files changed, 78 insertions(+), 7 deletions(-)
 create mode 120000 riscv/sieve.c
lib/riscv/asm/io.h +6 -0
index 6fe111289102..37a130e533c9 100644
@@ -76,6 +76,12 @@ static inline u64 __raw_readq(const volatile void __iomem *addr)
 #define ioremap ioremap
 void __iomem *ioremap(phys_addr_t phys_addr, size_t size);
 
+#define virt_to_phys virt_to_phys
+unsigned long virt_to_phys(volatile void *address);
+
+#define phys_to_virt phys_to_virt
+void *phys_to_virt(unsigned long address);
+
 #include <asm-generic/io.h>
 
 #endif /* _ASMRISCV_IO_H_ */
lib/riscv/asm/mmu.h +0 -2
index 02703f607511..66e993800b45 100644
@@ -18,8 +18,6 @@ void __mmu_enable(unsigned long satp);
 void mmu_enable(unsigned long mode, pgd_t *pgtable);
 void mmu_disable(void);
 
-void setup_mmu(void);
-
 static inline void local_flush_tlb_page(unsigned long addr)
 {
 	asm volatile("sfence.vma %0" : : "r" (addr) : "memory");
lib/riscv/mmu.c +56 -1

84 lines not on this page · raw message

lib/riscv/setup.c +13 -3
index c4c1bd58b337..40ff26a24cfc 100644
@@ -9,10 +9,12 @@
 #include <alloc_page.h>
 #include <alloc_phys.h>
 #include <argv.h>
+#include <auxinfo.h>
 #include <cpumask.h>
 #include <devicetree.h>
 #include <memregions.h>
 #include <on-cpus.h>
+#include <vmalloc.h>
 #include <asm/csr.h>
 #include <asm/mmu.h>
 #include <asm/page.h>
@@ -20,6 +22,11 @@
 #include <asm/setup.h>
 
 #define VA_BASE			((phys_addr_t)3 * SZ_1G)
+#if __riscv_xlen == 64
+#define VA_TOP			((phys_addr_t)4 * SZ_1G)
+#else
+#define VA_TOP			((phys_addr_t)0)
+#endif
 
 #define MAX_DT_MEM_REGIONS	16
 #define NR_MEM_REGIONS		(MAX_DT_MEM_REGIONS + 16)
@@ -106,6 +113,8 @@ static void mem_init(phys_addr_t freemem_start)
 		freemem_end = VA_BASE;
 	assert(freemem_end - freemem_start >= SZ_1M * 16);
 
+	init_alloc_vpage(__va(VA_TOP));
+
 	/*
 	 * TODO: Remove the need for this phys allocator dance, since, as we
 	 * can see with the assert, we could have gone straight to the page
@@ -137,7 +146,7 @@ void setup(const void *fdt, phys_addr_t freemem_start)
 	int ret;
 
 	assert(sizeof(long) == 8 || freemem_start < VA_BASE);
-	freemem = (void *)(unsigned long)freemem_start;
+	freemem = __va(freemem_start);
 
 	/* Move the FDT to the base of free memory */
 	fdt_size = fdt_totalsize(fdt);
@@ -156,7 +165,7 @@ void setup(const void *fdt, phys_addr_t freemem_start)
 		freemem += initrd_size;
 	}
 
-	mem_init(PAGE_ALIGN((unsigned long)freemem));
+	mem_init(PAGE_ALIGN(__pa(freemem)));
 	cpu_init();
 	thread_info_init();
 	io_init();
@@ -172,7 +181,8 @@ void setup(const void *fdt, phys_addr_t freemem_start)
 		setup_env(env, initrd_size);
 	}
 
-	setup_mmu();
+	if (!(auxinfo.flags & AUXINFO_MMU_OFF))
+		setup_vm();
 
 	banner();
 }
riscv/Makefile +2 -1
index 821891b719e7..61a1ff88d8ec 100644
@@ -13,7 +13,7 @@ endif
 tests =
 tests += $(TEST_DIR)/sbi.$(exe)
 tests += $(TEST_DIR)/selftest.$(exe)
-#tests += $(TEST_DIR)/sieve.$(exe)
+tests += $(TEST_DIR)/sieve.$(exe)
 
 all: $(tests)
 
@@ -27,6 +27,7 @@ cflatobjs += lib/alloc_phys.o
 cflatobjs += lib/devicetree.o
 cflatobjs += lib/memregions.o
 cflatobjs += lib/on-cpus.o
+cflatobjs += lib/vmalloc.o
 cflatobjs += lib/riscv/bitops.o
 cflatobjs += lib/riscv/io.o
 cflatobjs += lib/riscv/mmu.o
riscv/sieve.c new file +1 -0
new file mode 120000
index 000000000000..8f14a5c3d4aa
@@ -0,0 +1 @@
+../x86/sieve.c
\ No newline@end of file
Signature · 1 line
2.43.0
#24 lore raw

PATCH 21/24 lib: Add strcasecmp and strncasecmp

In reply to Andrew Jones

We'll soon want a case insensitive string comparison. Add toupper() and tolower() too (the latter gets used by the new string functions).

---
 lib/ctype.h  | 10 ++++++++++
 lib/string.c | 14 ++++++++++++++
 lib/string.h |  2 ++
 3 files changed, 26 insertions(+)
lib/ctype.h +10 -0
index 48a9c16300f8..45c96f111e19 100644
@@ -37,4 +37,14 @@ static inline int isspace(int c)
         return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\v' || c == '\f';
 }
 
+static inline int toupper(int c)
+{
+	return islower(c) ? c - 'a' + 'A' : c;
+}
+
+static inline int tolower(int c)
+{
+	return isupper(c) ? c - 'A' + 'a' : c;
+}
+
 #endif /* _CTYPE_H_ */
lib/string.c +14 -0
index 6d8a6380db92..ab6a724a3144 100644
@@ -54,11 +54,25 @@ int strncmp(const char *a, const char *b, size_t n)
 	return 0;
 }
 
+int strncasecmp(const char *a, const char *b, size_t n)
+{
+	for (; n--; ++a, ++b)
+		if (tolower(*a) != tolower(*b) || *a == '\0')
+			return tolower(*a) - tolower(*b);
+
+	return 0;
+}
+
 int strcmp(const char *a, const char *b)
 {
 	return strncmp(a, b, SIZE_MAX);
 }
 
+int strcasecmp(const char *a, const char *b)
+{
+	return strncasecmp(a, b, SIZE_MAX);
+}
+
 char *strchr(const char *s, int c)
 {
 	while (*s != (char)c)
lib/string.h +2 -0
index 758dca8af36a..a28d75641530 100644
@@ -15,6 +15,8 @@ extern char *strcat(char *dest, const char *src);
 extern char *strcpy(char *dest, const char *src);
 extern int strcmp(const char *a, const char *b);
 extern int strncmp(const char *a, const char *b, size_t n);
+int strcasecmp(const char *a, const char *b);
+int strncasecmp(const char *a, const char *b, size_t n);
 extern char *strchr(const char *s, int c);
 extern char *strrchr(const char *s, int c);
 extern char *strchrnul(const char *s, int c);
Signature · 1 line
2.43.0
Reviewed-by on v1 21/24
#25 lore raw

Re: [kvm-unit-tests PATCH 21/24] lib: Add strcasecmp and strncasecmp

Quoted 9 lines · Andrew Jones

We'll soon want a case insensitive string comparison. Add toupper() and tolower() too (the latter gets used by the new string functions).

---
  lib/ctype.h  | 10 ++++++++++
  lib/string.c | 14 ++++++++++++++
  lib/string.h |  2 ++
  3 files changed, 26 insertions(+)
#26 lore raw

PATCH 22/24 riscv: Add isa string parsing

In reply to Andrew Jones

We can probably get away with just assuming several important and popular extensions (at least everything covered by G), but we'll also want to use some extensions which we should ensure are present by parsing the isa string. Add a parser and already apply it to Sstc.

---
 lib/riscv/asm/isa.h       | 18 +++++++++
 lib/riscv/asm/processor.h |  1 +
 lib/riscv/processor.c     | 84 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 103 insertions(+)
 create mode 100644 lib/riscv/asm/isa.h
lib/riscv/asm/isa.h new file +18 -0
new file mode 100644
index 000000000000..4cb467b77077
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASMRISCV_ISA_H_
+#define _ASMRISCV_ISA_H_
+#include <bitops.h>
+#include <asm/setup.h>
+
+enum {
+	ISA_SSTC,
+	ISA_MAX,
+};
+_Static_assert(ISA_MAX <= __riscv_xlen, "Need to increase thread_info.isa");
+
+static inline bool cpu_has_extension(int cpu, int ext)
+{
+	return test_bit(ext, cpus[cpu].isa);
+}
+
+#endif /* _ASMRISCV_ISA_H_ */
lib/riscv/asm/processor.h +1 -0
index d8b7018c9102..928a988b471a 100644
@@ -11,6 +11,7 @@ typedef void (*exception_fn)(struct pt_regs *);
 struct thread_info {
 	int cpu;
 	unsigned long hartid;
+	unsigned long isa[1];
 	exception_fn exception_handlers[EXCEPTION_CAUSE_MAX];
 };
 
lib/riscv/processor.c +84 -0

101 lines not on this page · raw message

Signature · 1 line
2.43.0
#27 lore raw

PATCH 23/24 gitlab-ci: Add riscv64 tests

In reply to Andrew Jones

Add build/run tests for riscv64. We would also add riscv32, but Fedora doesn't package what we need for that.

---
 .gitlab-ci.yml | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
.gitlab-ci.yml +16 -0
index 273ec9a7224b..f3ec551a50f2 100644
@@ -87,6 +87,22 @@ build-ppc64le:
      | tee results.txt
  - if grep -q FAIL results.txt ; then exit 1 ; fi
 
+# build-riscv32:
+# Fedora doesn't package a riscv32 compiler for QEMU. Oh, well.
+
+build-riscv64:
+ extends: .intree_template
+ script:
+ - dnf install -y qemu-system-riscv gcc-riscv64-linux-gnu
+ - ./configure --arch=riscv64 --cross-prefix=riscv64-linux-gnu-
+ - make -j2
+ - printf "FOO=foo\nBAR=bar\nBAZ=baz\nMVENDORID=0\n" >test-env
+ - ACCEL=tcg KVM_UNIT_TESTS_ENV=test-env ./run_tests.sh
+      selftest
+      sbi
+      | tee results.txt
+ - if grep -q FAIL results.txt ; then exit 1 ; fi
+
 build-s390x:
  extends: .outoftree_template
  script:
Signature · 1 line
2.43.0
#28 lore raw

Re: [kvm-unit-tests PATCH 23/24] gitlab-ci: Add riscv64 tests

Quoted 21 lines · Andrew Jones

Add build/run tests for riscv64. We would also add riscv32, but Fedora doesn't package what we need for that.

---
  .gitlab-ci.yml | 16 ++++++++++++++++
  1 file changed, 16 insertions(+)
.gitlab-ci.yml +5 -0
index 273ec9a7224b..f3ec551a50f2 100644
@@ -87,6 +87,22 @@ build-ppc64le:
       | tee results.txt
   - if grep -q FAIL results.txt ; then exit 1 ; fi
 
+# build-riscv32:
+# Fedora doesn't package a riscv32 compiler for QEMU. Oh, well.
+
+build-riscv64:
+ extends: .intree_template

.gitlab-ci.yml build-ppc64le: · line 104

Quoted 10 lines
.gitlab-ci.yml +10 -0
+ script:
+ - dnf install -y qemu-system-riscv gcc-riscv64-linux-gnu
+ - ./configure --arch=riscv64 --cross-prefix=riscv64-linux-gnu-
+ - make -j2
+ - printf "FOO=foo\nBAR=bar\nBAZ=baz\nMVENDORID=0\n" >test-env
+ - ACCEL=tcg KVM_UNIT_TESTS_ENV=test-env ./run_tests.sh
+      selftest
+      sbi
+      | tee results.txt
+ - if grep -q FAIL results.txt ; then exit 1 ; fi

IIRC it's better to make sure that at least one test passed:

- grep -q PASS results.txt && ! grep -q FAIL results.txt

Otherwise all tests could be SKIP which indicates that something went wrong, too.
We're using the check for PASS for some tests in the gitlab-ci.yml file, but not for all ... we should maybe update the remaining ones to use that, too...

Thomas

#29 lore raw

Re: Re: [kvm-unit-tests PATCH 23/24] gitlab-ci: Add riscv64 tests

Quoted 34 lines · Thomas Huth
Quoted 20 lines · Andrew Jones

Add build/run tests for riscv64. We would also add riscv32, but Fedora doesn't package what we need for that.

---
  .gitlab-ci.yml | 16 ++++++++++++++++
  1 file changed, 16 insertions(+)
.gitlab-ci.yml +5 -0
index 273ec9a7224b..f3ec551a50f2 100644
@@ -87,6 +87,22 @@ build-ppc64le:
       | tee results.txt
   - if grep -q FAIL results.txt ; then exit 1 ; fi
+# build-riscv32:
+# Fedora doesn't package a riscv32 compiler for QEMU. Oh, well.
+
+build-riscv64:
+ extends: .intree_template

.gitlab-ci.yml build-ppc64le: · line 103

Quoted 10 lines
.gitlab-ci.yml +10 -0
+ script:
+ - dnf install -y qemu-system-riscv gcc-riscv64-linux-gnu
+ - ./configure --arch=riscv64 --cross-prefix=riscv64-linux-gnu-
+ - make -j2
+ - printf "FOO=foo\nBAR=bar\nBAZ=baz\nMVENDORID=0\n" >test-env
+ - ACCEL=tcg KVM_UNIT_TESTS_ENV=test-env ./run_tests.sh
+      selftest
+      sbi
+      | tee results.txt
+ - if grep -q FAIL results.txt ; then exit 1 ; fi

IIRC it's better to make sure that at least one test passed:

Quoted 4 lines

- grep -q PASS results.txt && ! grep -q FAIL results.txt

Otherwise all tests could be SKIP which indicates that something went wrong, too.

Yeah, for sure the basic selftests should pass.

Quoted 3 lines

We're using the check for PASS for some tests in the gitlab-ci.yml file, but not for all ... we should maybe update the remaining ones to use that, too...

Yup, almost all of them are the FAIL pattern which I copy+pasted. If I respin this series I'll change riscv now, but since I think we should change the other ones in a separate patch, then we could change riscv at the same time as the rest.

Thanks,
drew

#30 lore raw

PATCH 24/24 MAINTAINERS: Add riscv

In reply to Andrew Jones

Most of the support for riscv is now in place. Let's make it official and start adding tests!

---
 MAINTAINERS | 8 ++++++++
 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
index 958735fbfd79..a2fa437da5c9 100644
@@ -94,6 +94,14 @@ F: powerpc/
 F: lib/powerpc/
 F: lib/ppc64/
 
+RISCV
+M: Andrew Jones <[email protected]>
+S: Supported
+L: kvm-riscv at lists.infradead.org
+F: riscv/
+F: lib/riscv/
+T: https://gitlab.com/jones-drew/kvm-unit-tests.git
+
 S390X
 M: Janosch Frank <[email protected]>
 M: Claudio Imbrenda <[email protected]>
Signature · 1 line
2.43.0
Acked-by on the v1 series
#32 lore raw
In reply to Andrew Jones
Quoted 13 lines · Andrew Jones

This series adds another architecture to kvm-unit-tests (RISC-V, both 32-bit and 64-bit). Much of the code is borrowed from arm/arm64 by mimicking its patterns or by first making the arm code more generic and moving it to the common lib.

This series brings UART, SMP, MMU, and exception handling support. One should be able to start writing CPU validation tests in a mix of C and asm as well as write SBI tests, as is the plan for the SBI verification framework. kvm-unit-tests provides backtraces on asserts and input can be given to the tests through command line arguments, environment variables, and the DT (there's already an ISA string parser for extension detection).

Quoted 7 lines

This series only targets QEMU TCG and KVM, but OpenSBI may be replaced with other SBI implementations, such as RustSBI. It's a goal to target bare-metal as soon as possible, so EFI support is already in progress and will be posted soon. More follow on series will come as well, bringing interrupt controller support for timer and PMU testing, support to run tests in usermode, and whatever else people need for their tests.

I can't say much about the riscv details, but I very quickly skimmed through the series and I'm fine if we add that to the k-u-t, so FWIW:

Series