Skip to contentSkip to conversation

tests/tcg: more capabilities for aarch64-softmmu tests

PATCH In review Alex Bennée 3 participants 13 messages Apr 17 – May 7

lore
Share & tools
lore
#0 lore raw

cover tests/tcg: more capabilities for aarch64-softmmu tests

In the process of writing the WFx code I needed to enhance the support code so we could have interrupts in the test. To do that I needed to map hardware into the page tables. To do that I wanted to make the page table code easier to follow and now here we are.

It's quite a lot of code for a simple thing and of course if we keep going down this road we'll end up with a kernel all of our own although not big and professional like Linux.

The other option would be to build some machinery to exploit our cross compiler support and just import kvm-unit-tests somehow and maybe integrate it with meson more properly. It already has a more mature set of helpers and is set up for even more architectural shenanigans including setup for both GICs and a cleaner unit test wrappers.

While earlier versions of the tests where the result of LLM prototyping hopefully the splitting and refactoring has made it more human. There is an additional test for the HPFAR_EL2.NS for bug 2568 but I haven't had a chance to properly clean it up yet so I haven't included it here.

What do people think? Should we go down this road or choose the kvm-unit-tests option?

Alex.

Alex Bennée (7):
tests/tcg: introduce a lib dir for aarch64 system tests tests/tcg: convert aarch64 feat-xs test to use helpers tests/tcg: convert aarch64 asid2 test to use sysreg helpers tests/tcg: move aarch64 page table setup to c code tests/tcg: add HW page for aarch64 tests tests/tcg: create a mini-gic3 library
tests/tcg: add basic test for aarch64 wf[ie][t] insns

 tests/tcg/aarch64/system/lib/gicv3.h      |  60 +++++++++
 tests/tcg/aarch64/system/lib/pgtable.h    |  80 ++++++++++++
 tests/tcg/aarch64/system/lib/sysregs.h    |  42 +++++++
 tests/tcg/aarch64/system/asid2.c          |  35 +++---
 tests/tcg/aarch64/system/feat-xs.c        |   4 +-
 tests/tcg/aarch64/system/lib/gicv3.c      |  70 +++++++++++
 tests/tcg/aarch64/system/lib/pgtable.c    |  70 +++++++++++
 tests/tcg/aarch64/system/vtimer.c         |  23 +---
 tests/tcg/aarch64/system/wfx.c            | 143 ++++++++++++++++++++++
 tests/tcg/aarch64/Makefile.softmmu-target |  22 +++-
 tests/tcg/aarch64/system/boot.S           | 125 +++----------------
 tests/tcg/aarch64/system/kernel.ld        |   3 +
 12 files changed, 521 insertions(+), 156 deletions(-)
 create mode 100644 tests/tcg/aarch64/system/lib/gicv3.h
 create mode 100644 tests/tcg/aarch64/system/lib/pgtable.h
 create mode 100644 tests/tcg/aarch64/system/lib/sysregs.h
 create mode 100644 tests/tcg/aarch64/system/lib/gicv3.c
 create mode 100644 tests/tcg/aarch64/system/lib/pgtable.c
 create mode 100644 tests/tcg/aarch64/system/wfx.c
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/7 tests/tcg: introduce a lib dir for aarch64 system tests

We are about to add some more helpers but lets start by adding a simple wrapper for reading and writing system registers.

Do note the subtle switch of ordering for write_sysreg() which matches the change the kernel made sometime in 2021 (see 272a067df in linux.git).

---
 tests/tcg/aarch64/system/lib/sysregs.h    | 22 ++++++++++++++++++++++
 tests/tcg/aarch64/system/vtimer.c         | 23 ++++-------------------
 tests/tcg/aarch64/Makefile.softmmu-target |  3 ++-
 3 files changed, 28 insertions(+), 20 deletions(-)
 create mode 100644 tests/tcg/aarch64/system/lib/sysregs.h
tests/tcg/aarch64/system/lib/sysregs.h new file +22 -0
new file mode 100644
index 00000000000..b1c465e0746
@@ -0,0 +1,22 @@
+/*
+ * AArch64 system register helpers
+ *
+ * Based on the helpers from Linux
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#define __stringify_1(x...) #x
+#define __stringify(x...)   __stringify_1(x)
+
+#define read_sysreg(r) ({                                           \
+            uint64_t __val;                                         \
+            asm volatile("mrs %0, " __stringify(r) : "=r" (__val)); \
+            __val;                                                  \
+})
+
+#define write_sysreg(v, r) do {                     \
+        uint64_t __val = (uint64_t)(v);             \
+        asm volatile("msr " __stringify(r) ", %x0"  \
+                 : : "rZ" (__val));                 \
+} while (0)
tests/tcg/aarch64/system/vtimer.c +4 -19
index 7d725eced34..b6bde49ce30 100644
@@ -8,22 +8,7 @@
 
 #include <stdint.h>
 #include <minilib.h>
-
-/* grabbed from Linux */
-#define __stringify_1(x...) #x
-#define __stringify(x...)   __stringify_1(x)
-
-#define read_sysreg(r) ({                                           \
-            uint64_t __val;                                         \
-            asm volatile("mrs %0, " __stringify(r) : "=r" (__val)); \
-            __val;                                                  \
-})
-
-#define write_sysreg(r, v) do {                     \
-        uint64_t __val = (uint64_t)(v);             \
-        asm volatile("msr " __stringify(r) ", %x0"  \
-                 : : "rZ" (__val));                 \
-} while (0)
+#include "sysregs.h"
 
 int main(void)
 {
@@ -31,9 +16,9 @@ int main(void)
 
     ml_printf("VTimer Test\n");
 
-    write_sysreg(cntvoff_el2, 1);
-    write_sysreg(cntv_cval_el0, -1);
-    write_sysreg(cntv_ctl_el0, 1);
+    write_sysreg(1, cntvoff_el2);
+    write_sysreg(-1, cntv_cval_el0);
+    write_sysreg(1, cntv_ctl_el0);
 
     ml_printf("cntvoff_el2=%lx\n", read_sysreg(cntvoff_el2));
     ml_printf("cntv_cval_el0=%lx\n", read_sysreg(cntv_cval_el0));
tests/tcg/aarch64/Makefile.softmmu-target +2 -1
index f7a7d2b800f..c7f44803c73 100644
@@ -4,6 +4,7 @@
 
 AARCH64_SRC=$(SRC_PATH)/tests/tcg/aarch64
 AARCH64_SYSTEM_SRC=$(AARCH64_SRC)/system
+AARCH64_SYSTEM_LIB=$(AARCH64_SRC)/system/lib
 
 VPATH+=$(AARCH64_SYSTEM_SRC)
 
@@ -24,7 +25,7 @@ LINK_SCRIPT=$(AARCH64_SYSTEM_SRC)/kernel.ld
 LDFLAGS=-Wl,-T$(LINK_SCRIPT)
 TESTS+=$(AARCH64_TESTS) $(MULTIARCH_TESTS)
 EXTRA_RUNS+=$(MULTIARCH_RUNS)
-CFLAGS+=-nostdlib -ggdb -O0 $(MINILIB_INC)
+CFLAGS+=-nostdlib -ggdb -O0 $(MINILIB_INC) -I $(AARCH64_SYSTEM_LIB)
 LDFLAGS+=-static -nostdlib $(CRT_OBJS) $(MINILIB_OBJS) -lgcc
 
 config-cc.mak: Makefile
Signature · 1 line
2.47.3
Reviewed-by on v1 1/7
#2 lore raw

Re: [PATCH 1/7] tests/tcg: introduce a lib dir for aarch64 system tests

Quoted 6 lines · Alex Bennée

We are about to add some more helpers but lets start by adding a simple wrapper for reading and writing system registers.

Do note the subtle switch of ordering for write_sysreg() which matches the change the kernel made sometime in 2021 (see 272a067df in linux.git).

Quoted 7 lines
---
  tests/tcg/aarch64/system/lib/sysregs.h    | 22 ++++++++++++++++++++++
  tests/tcg/aarch64/system/vtimer.c         | 23 ++++-------------------
  tests/tcg/aarch64/Makefile.softmmu-target |  3 ++-
  3 files changed, 28 insertions(+), 20 deletions(-)

create mode 100644 tests/tcg/aarch64/system/lib/sysregs.h

r~

#3 lore raw

PATCH 2/7 tests/tcg: convert aarch64 feat-xs test to use helpers

In reply to Alex Bennée
---
 tests/tcg/aarch64/system/feat-xs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/tcg/aarch64/system/feat-xs.c +2 -2
index f310fc837e0..e2324cef1af 100644
@@ -8,12 +8,12 @@
 
 #include <minilib.h>
 #include <stdint.h>
+#include "sysregs.h"
 
 int main(void)
 {
-    uint64_t isar1;
+    uint64_t isar1 = read_sysreg(id_aa64isar1_el1);
 
-    asm volatile ("mrs %0, id_aa64isar1_el1" : "=r"(isar1));
     if (((isar1 >> 56) & 0xf) < 1) {
         ml_printf("FEAT_XS not supported by CPU");
         return 1;
Signature · 1 line
2.47.3
Reviewed-by on v1 2/7
#4 lore raw

Re: [PATCH 2/7] tests/tcg: convert aarch64 feat-xs test to use helpers

Quoted 14 lines · Alex Bennée
---
  tests/tcg/aarch64/system/feat-xs.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tests/tcg/aarch64/system/feat-xs.c +1 -0
index f310fc837e0..e2324cef1af 100644
@@ -8,12 +8,12 @@
 
  #include <minilib.h>
  #include <stdint.h>
+#include "sysregs.h"

tests/tcg/aarch64/system/feat-xs.c line 19

Quoted 10 lines
tests/tcg/aarch64/system/feat-xs.c +1 -2
 
  int main(void)
  {
-    uint64_t isar1;
+    uint64_t isar1 = read_sysreg(id_aa64isar1_el1);
 
-    asm volatile ("mrs %0, id_aa64isar1_el1" : "=r"(isar1));
      if (((isar1 >> 56) & 0xf) < 1) {
          ml_printf("FEAT_XS not supported by CPU");
          return 1;

r~

#5 lore raw

PATCH 3/7 tests/tcg: convert aarch64 asid2 test to use sysreg helpers

In reply to Alex Bennée

While we are at it we can reduce the number of temporary variables needed and fix a format string error.

---
 tests/tcg/aarch64/system/asid2.c | 35 ++++++++++++++------------------
 1 file changed, 15 insertions(+), 20 deletions(-)
tests/tcg/aarch64/system/asid2.c +15 -20
index 7d5466af34a..8c93024b858 100644
@@ -9,10 +9,11 @@
 
 #include <stdint.h>
 #include <minilib.h>
+#include "sysregs.h"
 
-#define ID_AA64MMFR3_EL1 "S3_0_C0_C7_3"
-#define ID_AA64MMFR4_EL1 "S3_0_C0_C7_4"
-#define TCR2_EL1 "S3_0_C2_C0_3"
+#define ID_AA64MMFR3_EL1 S3_0_C0_C7_3
+#define ID_AA64MMFR4_EL1 S3_0_C0_C7_4
+#define TCR2_EL1 S3_0_C2_C0_3
 
 int main()
 {
@@ -23,7 +24,7 @@ int main()
      * not enabled and read as the written value if A2 is enabled.
      */
 
-    uint64_t out;
+    uint64_t read_tcr2;
     uint64_t idreg3;
     uint64_t idreg4;
     int tcr2_present;
@@ -31,11 +32,8 @@ int main()
 
     /* Mask is FNG1, FNG0, and A2 */
     const uint64_t feature_mask = (1ULL << 18 | 1ULL << 17 | 1ULL << 16);
-    const uint64_t in = feature_mask;
-
-    asm("mrs %[idreg3], " ID_AA64MMFR3_EL1 "\n\t"
-        : [idreg3] "=r" (idreg3));
 
+    idreg3 = read_sysreg(ID_AA64MMFR3_EL1);
     tcr2_present = ((idreg3 & 0xF) != 0);
 
     if (!tcr2_present) {
@@ -43,33 +41,30 @@ int main()
         return 0;
     }
 
-    asm("mrs %[idreg4], " ID_AA64MMFR4_EL1 "\n\t"
-        : [idreg4] "=r" (idreg4));
-
+    idreg4 = read_sysreg(ID_AA64MMFR4_EL1);
     asid2_present = ((idreg4 & 0xF00) != 0);
 
-    asm("msr " TCR2_EL1 ", %[x0]\n\t"
-        "mrs %[x1], " TCR2_EL1 "\n\t"
-        : [x1] "=r" (out)
-        : [x0] "r" (in));
+    /* write the feature mask and read back */
+    write_sysreg(feature_mask, TCR2_EL1);
+    read_tcr2 = read_sysreg(TCR2_EL1);
 
     if (asid2_present) {
-        if ((out & feature_mask) == in) {
+        if ((read_tcr2 & feature_mask) == feature_mask) {
             ml_printf("OK\n");
             return 0;
         } else {
             ml_printf("FAIL: ASID2 present, but read value %lx != "
                       "written value %lx\n",
-                      out & feature_mask, in);
+                      read_tcr2 & feature_mask, feature_mask);
             return 1;
         }
     } else {
-        if (out == 0) {
+        if (read_tcr2 == 0) {
             ml_printf("TCR2_EL1 reads as RES0 as expected\n");
             return 0;
         } else {
-            ml_printf("FAIL: ASID2, missing but read value %lx != 0\n",
-                      out & feature_mask, in);
+            ml_printf("FAIL: ASID2, missing but read value %lx != %lx\n",
+                      read_tcr2 & feature_mask, feature_mask);
             return 1;
         }
     }
Signature · 1 line
2.47.3
Reviewed-by on v1 3/7
#6 lore raw

Re: [PATCH 3/7] tests/tcg: convert aarch64 asid2 test to use sysreg helpers

Quoted 7 lines · Alex Bennée

While we are at it we can reduce the number of temporary variables needed and fix a format string error.

---
  tests/tcg/aarch64/system/asid2.c | 35 ++++++++++++++------------------
  1 file changed, 15 insertions(+), 20 deletions(-)

r~

#7 lore raw

PATCH 4/7 tests/tcg: move aarch64 page table setup to c code

In reply to Alex Bennée

If we want more flexibility messing around with page tables lets do it without having to rely on assembly for it.

---
AJB
- a result of -O0 does mean each helper ends up with a stack frame being needed to do the relatively simple mask and shift. I've experimented with -O1 and trying to manually enable inlining but couldn't get anything to work.

---
 tests/tcg/aarch64/system/lib/pgtable.h    |  80 ++++++++++++++++
 tests/tcg/aarch64/system/lib/sysregs.h    |  20 ++++
 tests/tcg/aarch64/system/lib/pgtable.c    |  60 ++++++++++++
 tests/tcg/aarch64/Makefile.softmmu-target |   9 +-
 tests/tcg/aarch64/system/boot.S           | 109 +---------------------
 tests/tcg/aarch64/system/kernel.ld        |   3 +
 6 files changed, 170 insertions(+), 111 deletions(-)
 create mode 100644 tests/tcg/aarch64/system/lib/pgtable.h
 create mode 100644 tests/tcg/aarch64/system/lib/pgtable.c
tests/tcg/aarch64/system/lib/pgtable.h new file +80 -0
new file mode 100644
index 00000000000..c5048c3eea1
@@ -0,0 +1,80 @@
+/*
+ * AArch64 page table helpers
+ *
+ * Some simple helper functions for setting the page table entries.
+ *
+ * Copyright (C) 2026 Linaro Ltd.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+/*
+ * Page Table Descriptors
+ */
+#define DESC_VALID       (1ULL << 0)
+#define DESC_TYPE_TABLE  (1ULL << 1)
+#define DESC_TYPE_BLOCK  (0ULL << 1)
+#define DESC_TYPE_PAGE   (1ULL << 1)
+
+#define DESC_AF          (1ULL << 10)
+#define DESC_ATTRINDX(i) ((uint64_t)(i) << 2)
+#define DESC_NS          (1ULL << 5)
+#define DESC_UXN         (1ULL << 53)
+#define DESC_PXN         (1ULL << 54)
+
+#define DESC_ADDR_MASK          0x0000FFFFFFFFF000ULL
+#define DESC_ADDR_BLOCK_L1      0x0000FFFFC0000000ULL
+#define DESC_ADDR_BLOCK_L2      0x0000FFFFFFE00000ULL
+
+/* Stage 2 specific */
+#define DESC_S2_AP_RW           (3ULL << 6)
+#define DESC_S2_MEMATTR_NORMAL  (0xfULL << 2)
+
+/**
+ * pgt_map_l1_table - Setup a Level 1 table pointing to a Level 2 table
+ * @table: the level 1 table
+ * @va: the virtual address to map
+ * @next_table: the level 2 table to point to
+ */
+static inline void pgt_map_l1_table(uint64_t *table, uintptr_t va, uint64_t *next_table)
+{
+    int index = (va >> 30) & 0x1ff;
+    table[index] = ((uintptr_t)next_table & DESC_ADDR_MASK) | DESC_TYPE_TABLE | DESC_VALID;
+}
+
+/**
+ * pgt_map_l1_block - Setup a Level 1 block mapping (1GB)
+ * @table: the level 1 table
+ * @va: the virtual address to map
+ * @pa: the physical address to map to
+ * @flags: the descriptor flags (e.g. DESC_AF | ...)
+ */
+static inline void pgt_map_l1_block(uint64_t *table, uintptr_t va, uintptr_t pa, uint64_t flags)
+{
+    int index = (va >> 30) & 0x1ff;
+    table[index] = (pa & DESC_ADDR_BLOCK_L1) | flags | DESC_TYPE_BLOCK | DESC_VALID;
+}
+
+/**
+ * pgt_map_l2_block - Setup a Level 2 block mapping (2MB)
+ * @table: the level 2 table
+ * @va: the virtual address to map
+ * @pa: the physical address to map to
+ * @flags: the descriptor flags (e.g. DESC_AF | ...)
+ */
+static inline void pgt_map_l2_block(uint64_t *table, uintptr_t va, uintptr_t pa, uint64_t flags)
+{
+    int index = (va >> 21) & 0x1ff;
+    table[index] = (pa & DESC_ADDR_BLOCK_L2) | flags | DESC_TYPE_BLOCK | DESC_VALID;
+}
+
+/**
+ * flat_map_stage2 - Setup a flat (VA==PA) stage 2 mapping
+ * @table: the level 2 table
+ * @addr: the VA/PA to map
+ * @flags: the descriptor flags (e.g. DESC_AF | ...)
+ */
+static inline void flat_map_stage2(uint64_t *table, uintptr_t addr, uint64_t flags)
+{
+    pgt_map_l2_block(table, addr, addr, flags);
+}
tests/tcg/aarch64/system/lib/sysregs.h +20 -0
index b1c465e0746..6e714f46842 100644
@@ -20,3 +20,23 @@
         asm volatile("msr " __stringify(r) ", %x0"  \
                  : : "rZ" (__val));                 \
 } while (0)
+
+#define isb() asm volatile("isb" : : : "memory")
+#define dsb(opt) asm volatile("dsb " #opt : : : "memory")
+
+/*
+ * SCTLR_EL1 Bits
+ */
+#define SCTLR_EL1_M      (1ULL << 0)  /* enable MMU for EL0/1 */
+#define SCTLR_EL1_C      (1ULL << 2)  /* Data cachability control */
+#define SCTLR_EL1_SA     (1ULL << 3)  /* SP alignment check */
+#define SCTLR_EL1_I      (1ULL << 12) /* Instruction cachability control */
+
+/*
+ * TCR_EL1 Bits
+ */
+#define TCR_EL1_T0SZ(s)  ((s) & 0x3fULL)
+#define TCR_EL1_IRGN0_WBWA (3ULL << 8)
+#define TCR_EL1_ORGN0_WBWA (3ULL << 10)
+#define TCR_EL1_IPS_40BIT  (2ULL << 32)
+#define TCR_EL1_TG0_4KB    (0ULL << 14)
tests/tcg/aarch64/system/lib/pgtable.c new file +60 -0

60 lines not on this page · raw message

tests/tcg/aarch64/Makefile.softmmu-target +6 -3
index c7f44803c73..5770ae1fb0c 100644
@@ -6,10 +6,10 @@ AARCH64_SRC=$(SRC_PATH)/tests/tcg/aarch64
 AARCH64_SYSTEM_SRC=$(AARCH64_SRC)/system
 AARCH64_SYSTEM_LIB=$(AARCH64_SRC)/system/lib
 
-VPATH+=$(AARCH64_SYSTEM_SRC)
+VPATH+=$(AARCH64_SYSTEM_SRC) $(AARCH64_SYSTEM_LIB)
 
 # These objects provide the basic boot code and helper functions for all tests
-CRT_OBJS=boot.o
+CRT_OBJS=boot.o pgtable.o
 
 AARCH64_TEST_C_SRCS=$(wildcard $(AARCH64_SYSTEM_SRC)/*.c)
 AARCH64_TEST_S_SRCS=$(AARCH64_SYSTEM_SRC)/mte.S
@@ -37,9 +37,12 @@ config-cc.mak: Makefile
 # building head blobs
 .PRECIOUS: $(CRT_OBJS)
 
-%.o: $(CRT_PATH)/%.S
+%.o: %.S
 	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -x assembler-with-cpp -Wa,--noexecstack -c $< -o $@
 
+%.o: %.c
+	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c $< -o $@
+
 # Build and link the tests
 %: %.c $(LINK_SCRIPT) $(CRT_OBJS) $(MINILIB_OBJS)
 	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $< -o $@ $(LDFLAGS)
tests/tcg/aarch64/system/boot.S +1 -108

121 lines not on this page · raw message

tests/tcg/aarch64/system/kernel.ld +3 -0
index aef043e31db..267ffcbe520 100644
@@ -13,10 +13,12 @@ MEMORY {
 
 SECTIONS {
     .text : {
+        _text_start = .;
         *(.text)
         *(.rodata)
     } >TXT
     .data : {
+        _data_start = .;
         *(.data)
         *(.bss)
     } >DAT
@@ -25,6 +27,7 @@ SECTIONS {
          * Symbol 'mte_page' is used in boot.S to setup the PTE and in the mte.S
          * test as the address that the MTE instructions operate on.
          */
+        _tag_start = .;
         mte_page = .;
     } >TAG
     /DISCARD/ : {
Signature · 1 line
2.47.3
Reviewed-by on v1 4/7
#8 lore raw

Re: [PATCH 4/7] tests/tcg: move aarch64 page table setup to c code

Quoted 12 lines · Alex Bennée

If we want more flexibility messing around with page tables lets do it without having to rely on assembly for it.

---
AJB
- a result of -O0 does mean each helper ends up with a stack frame being needed to do the relatively simple mask and shift. I've experimented with -O1 and trying to manually enable inlining but couldn't get anything to work.
---

tests/tcg/aarch64/system/lib/pgtable.h line 23

Quoted 5 lines
tests/tcg/aarch64/system/lib/pgtable.h new file +5 -0
+#define DESC_AF          (1ULL << 10)
+#define DESC_ATTRINDX(i) ((uint64_t)(i) << 2)
+#define DESC_NS          (1ULL << 5)
+#define DESC_UXN         (1ULL << 53)
+#define DESC_PXN         (1ULL << 54)

I *think* you have UXN and PXN the wrong way round? See table D8-52; UXN is always 54. It works fine in this case as we always use them together.

Otherwise,

#9 lore raw

PATCH 5/7 tests/tcg: add HW page for aarch64 tests

In reply to Alex Bennée

If the test cases need to program items like the GIC we need to define the region as Device-nGnRE memory. As this is for test cases just map the whole first GB block (which is all HW in the -M virt machine).

---
 tests/tcg/aarch64/system/lib/pgtable.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
tests/tcg/aarch64/system/lib/pgtable.c +17 -7
index a558f5f0dd5..5e7d95fd3d7 100644
@@ -17,14 +17,18 @@ uint64_t ttb_l1[512] __attribute__((aligned(4096)));
 uint64_t ttb_l2[512] __attribute__((aligned(4096)));
 
 /*
- * Setup a flat address mapping page-tables. Stage one simply
- * maps RAM to the first Gb. The stage2 tables have two 2mb
- * translation block entries covering a series of adjacent
- * 4k pages.
+ * Setup a flat address mapping page-tables.
+ *
+ * ttb (Level 1):
+ *   - Entry 0 [0 - 1GB]: 1GB Device block (for GIC and other H/W)
+ *   - Entry 1 [1GB - 2GB]: Table entry pointing to ttb_l2 (for RAM)
  */
 void setup_pgtables(void)
 {
-    /* L1 entry points to L2 table */
+    /* L1 entry 0: 1GB Device block mapping at 0x0 */
+    pgt_map_l1_block(ttb_l1, 0, 0, DESC_AF | DESC_ATTRINDX(2));
+
+    /* L1 entry 1: points to L2 table for finer permissions */
     pgt_map_l1_table(ttb_l1, (uintptr_t)_text_start, ttb_l2);
 
     /* L2 entries: 2MB blocks */
@@ -40,8 +44,14 @@ void setup_pgtables(void)
     /* Set TTBR0_EL1 */
     write_sysreg(ttb_l1, ttbr0_el1);
 
-    /* Set MAIR_EL1 */
-    write_sysreg(0xeeULL, mair_el1);
+    /*
+     * Set MAIR_EL1
+     *
+     * Attr0 (0xee): Normal memory, Outer/Inner WB/WA/Read-Alloc
+     * Attr1 (0x00): MTE page
+     * Attr2 (0x04): Device-nGnRE memory (for the first 1GB)
+     */
+    write_sysreg(0x0400eeULL, mair_el1);
 
     /* Set TCR_EL1 */
     uint64_t tcr = TCR_EL1_IPS_40BIT | TCR_EL1_TG0_4KB | TCR_EL1_ORGN0_WBWA | TCR_EL1_IRGN0_WBWA | TCR_EL1_T0SZ(25);
Signature · 1 line
2.47.3
#10 lore raw

PATCH 6/7 tests/tcg: create a mini-gic3 library

In reply to Alex Bennée

Just enough GIC to trigger timer interrupts.

---
 tests/tcg/aarch64/system/lib/gicv3.h      | 60 +++++++++++++++++++
 tests/tcg/aarch64/system/lib/gicv3.c      | 70 +++++++++++++++++++++++
 tests/tcg/aarch64/Makefile.softmmu-target |  2 +
 3 files changed, 132 insertions(+)
 create mode 100644 tests/tcg/aarch64/system/lib/gicv3.h
 create mode 100644 tests/tcg/aarch64/system/lib/gicv3.c
tests/tcg/aarch64/system/lib/gicv3.h new file +60 -0
new file mode 100644
index 00000000000..05c4c2f538b
@@ -0,0 +1,60 @@
+/*
+ * GICv3 Helper Library
+ *
+ * Copyright (c) 2026 Linaro Ltd
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef GICV3_H
+#define GICV3_H
+
+/*
+ * This duplicates just the bits of hw/intc/gicv3_internal.h we
+ * need for basic functionality.
+ */
+
+/* Virt machine GICv3 base addresses */
+#define GICD_BASE       0x08000000  /* c.f. VIRT_GIC_DIST */
+#define GICR_BASE       0x080a0000  /* c.f. VIRT_GIC_REDIST */
+
+/* Distributor registers */
+#define GICD_CTLR       (GICD_BASE + 0x0000)
+#define GICD_TYPER      (GICD_BASE + 0x0004)
+#define GICD_IIDR       (GICD_BASE + 0x0008)
+
+/* Redistributor registers (per-CPU) */
+#define GICR_SGI_OFFSET 0x00010000
+
+#define GICR_CTLR       0x0000
+#define GICR_WAKER      0x0014
+
+#define GICR_IGROUPR0   (GICR_SGI_OFFSET + 0x0080)
+#define GICR_ISENABLER0 (GICR_SGI_OFFSET + 0x0100)
+#define GICR_IPRIORITYR (GICR_SGI_OFFSET + 0x0400)
+
+/* GICD_CTLR fields */
+#define GICD_CTLR_ENA_GRP0   (1U << 0)
+#define GICD_CTLR_ENA_GRP1NS (1U << 1)
+#define GICD_CTLR_ARE_NS     (1U << 5)
+
+/* GICR_WAKER bits */
+#define GICR_WAKER_ProcessorSleep (1U << 1)
+#define GICR_WAKER_ChildrenAsleep (1U << 2)
+
+/**
+ * gicv3_init:
+ *
+ * Initialize GICv3 distributor and the redistributor for the current CPU.
+ */
+void gicv3_init(void);
+
+/**
+ * gicv3_enable_irq:
+ * @irq: The IRQ number to enable
+ *
+ * Enable the specified IRQ (SPI or PPI).
+ */
+void gicv3_enable_irq(unsigned int irq);
+
+#endif /* GICV3_H */
tests/tcg/aarch64/system/lib/gicv3.c new file +70 -0

70 lines not on this page · raw message

tests/tcg/aarch64/Makefile.softmmu-target +2 -0
index 5770ae1fb0c..543d638e819 100644
@@ -106,6 +106,8 @@ run-pauth-3:
 	$(call skip-test, "RUN of pauth-3", "not built")
 endif
 
+gicv3.o: gicv3.c gicv3.h
+	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c $< -o $@
 ifneq ($(CROSS_CC_HAS_ARMV8_MTE),)
 QEMU_MTE_ENABLED_MACHINE=-M virt,mte=on -cpu max -display none
 QEMU_OPTS_WITH_MTE_ON = $(QEMU_MTE_ENABLED_MACHINE) $(QEMU_BASE_ARGS) -kernel
Signature · 1 line
2.47.3
#11 lore raw

PATCH 7/7 tests/tcg: add basic test for aarch64 wf[ie][t] insns

In reply to Alex Bennée

This is based on a generated test case which I've since refined and cleaned up.

---
 tests/tcg/aarch64/system/wfx.c            | 143 ++++++++++++++++++++++
 tests/tcg/aarch64/Makefile.softmmu-target |   8 ++
 tests/tcg/aarch64/system/boot.S           |  16 ++-
 3 files changed, 164 insertions(+), 3 deletions(-)
 create mode 100644 tests/tcg/aarch64/system/wfx.c
tests/tcg/aarch64/system/wfx.c new file +143 -0
new file mode 100644
index 00000000000..fa4c6761215
@@ -0,0 +1,143 @@
+/*
+ * WFX Instructions Test (WFI, WFE, WFIT, WFET)
+ *
+ * Copyright (c) 2026 Linaro Ltd
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <minilib.h>
+#include "sysregs.h"
+#include "gicv3.h"
+
+#define TIMEOUT 200000
+
+#define sev() asm volatile("sev" : : : "memory")
+#define sevl() asm volatile("sevl" : : : "memory")
+#define wfi() asm volatile("wfi" : : : "memory")
+#define wfe() asm volatile("wfe" : : : "memory")
+#define wfit(reg) asm volatile("wfit %0" : : "r" (reg) : "memory")
+#define wfet(reg) asm volatile("wfet %0" : : "r" (reg) : "memory")
+
+#define enable_irq()  asm volatile("msr daifclr, #2" : : : "memory")
+#define disable_irq() asm volatile("msr daifset, #2" : : : "memory")
+
+static bool check_elapsed(uint64_t start, uint64_t threshold, const char *test, bool more)
+{
+    uint64_t end = read_sysreg(cntvct_el0);
+    uint64_t elapsed = end - start;
+    if (more ? elapsed < threshold : elapsed > threshold) {
+        ml_printf("FAILED: %s %s (%ld ticks)\n", test,
+                  more ? "woke too early" : "slept despite SEV",
+                  elapsed);
+        return false;
+    }
+    ml_printf("PASSED (%ld ticks)\n", elapsed);
+    return true;
+}
+
+int main(void)
+{
+    uint64_t start, timeout;
+
+    gicv3_init();
+    gicv3_enable_irq(27); /* Virtual Timer PPI */
+
+    ml_printf("WFx[T] Tests\n");
+
+    /*
+     * 1. Test WFI with timer interrupt
+     *
+     * We don't have a full interrupt handler, but WFI should wake up
+     * when the interrupt is pending even if we have it masked at the CPU.
+     * PSTATE.I is set by boot code.
+     *
+     * We unmask interrupts here to ensure the CPU can take the minimal
+     * exception handler defined in boot.S.
+     */
+    ml_printf("Testing WFI...");
+
+    start = read_sysreg(cntvct_el0);
+    write_sysreg(TIMEOUT, cntv_tval_el0);
+    write_sysreg(1, cntv_ctl_el0); /* Enable timer, no mask */
+    isb();
+
+    enable_irq();
+    wfi();
+    disable_irq();
+
+    if (!check_elapsed(start, TIMEOUT, "WFI", true)) {
+        return 1;
+    }
+
+    /* Validate the timer fired and then disable for future tests */
+    if (!read_sysreg(cntv_ctl_el0) & 0x4) {
+        ml_printf("Time ISTATUS not set!\n");
+        return 1;
+    }
+    write_sysreg(0, cntv_ctl_el0);
+
+    /*
+     * 2. Test WFE and SEV[L]
+     *
+     * There are two SEV instructions, the normal one is a broadcast
+     * from any PE on the system, the other is local only.
+     * Functionally they have the same effect (setting the event
+     * register) and should be immediately consumed by the WFE.
+     *
+     * As we want to detect an early exit the sense of the timeout
+     * check is reversed.
+     */
+    ml_printf("Testing WFE/SEV...");
+    sev();
+    start = read_sysreg(cntvct_el0);
+    wfe();
+    if (!check_elapsed(start, TIMEOUT, "WFE", false)) {
+        return 1;
+    }
+
+    ml_printf("Testing WFE/SEVL...");
+    sevl();
+    start = read_sysreg(cntvct_el0);
+    wfe();
+    if (!check_elapsed(start, TIMEOUT, "WFE", false)) {
+        return 1;
+    }
+
+    /*
+     * 3. Test WFIT
+     *
+     * With the timer now disabled and no other IRQ sources firing the
+     * WFIT instruction should timeout. Although the architecture
+     * permits this being treated as a NOP we have enabled it.
+     */
+    ml_printf("Testing WFIT...");
+    start = read_sysreg(cntvct_el0);
+    timeout = start + TIMEOUT;
+    wfit(timeout);
+    if (!check_elapsed(start, TIMEOUT, "WFIT", true)) {
+        return 1;
+    }
+
+    /*
+     * 4. Test WFET
+     *
+     * Much like WFIT there are no IRQs to wake us up. However the
+     * event_register is a latch so we must first consume the event
+     * register with a normal WFE before we do the timeout version.
+       */
+    ml_printf("Testing WFET...");
+    sev();
+    wfe();
+    start = read_sysreg(cntvct_el0);
+    timeout = start + TIMEOUT;
+    wfet(timeout);
+    if (!check_elapsed(start, TIMEOUT, "WFET", true)) {
+        return 1;
+    }
+
+    ml_printf("ALL WFX TESTS PASSED\n");
+    return 0;
+}
tests/tcg/aarch64/Makefile.softmmu-target +8 -0
index 543d638e819..6a1e867fba0 100644
@@ -108,6 +108,14 @@ endif
 
 gicv3.o: gicv3.c gicv3.h
 	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c $< -o $@
+
+wfx: CFLAGS += -march=armv8.7-a
+wfx: LDFLAGS += gicv3.o
+wfx: gicv3.o
+
+QEMU_GICV3_MACHINE=-M virt,gic-version=3 -cpu max -display none
+run-wfx: QEMU_OPTS=$(QEMU_GICV3_MACHINE) $(QEMU_BASE_ARGS) -kernel
+
 ifneq ($(CROSS_CC_HAS_ARMV8_MTE),)
 QEMU_MTE_ENABLED_MACHINE=-M virt,mte=on -cpu max -display none
 QEMU_OPTS_WITH_MTE_ON = $(QEMU_MTE_ENABLED_MACHINE) $(QEMU_BASE_ARGS) -kernel
tests/tcg/aarch64/system/boot.S +13 -3
index bee8d9372e7..ba9000a749d 100644
@@ -35,7 +35,7 @@ vector_table:
 
 	/* Current EL with SPx.	 */
 	ventry	curr_spx_sync		/* Synchronous	*/
-	ventry	curr_spx_irq		/* IRQ/vIRQ  */
+	ventry	curr_spx_irq		/* Basic handler for IRQ/vIRQ */
 	ventry	curr_spx_fiq		/* FIQ/vFIQ  */
 	ventry	curr_spx_serror		/* SError/VSError  */
 
@@ -54,13 +54,12 @@ vector_table:
 	.text
 	.align 4
 
-	/* Common vector handling for now */
+	/* Apart from curr_spx_irq we error out with an unexpected exception */
 curr_sp0_sync:
 curr_sp0_irq:
 curr_sp0_fiq:
 curr_sp0_serror:
 curr_spx_sync:
-curr_spx_irq:
 curr_spx_fiq:
 curr_spx_serror:
 lower_a64_sync:
@@ -275,6 +274,17 @@ _exit:
 	semihosting_call
 	/* never returns */
 
+	/*
+	 * IRQ handler
+	 */
+	.global curr_spx_irq
+curr_spx_irq:
+	/* Minimal IRQ handler: just mask the timer and return */
+	mrs	x0, cntv_ctl_el0
+	orr	x0, x0, #2		/* IMASK=1 */
+	msr	cntv_ctl_el0, x0
+	eret
+
 	/*
 	 * Helper Functions
 	*/
Signature · 1 line
2.47.3
#12 lore raw

Re: [PATCH 7/7] tests/tcg: add basic test for aarch64 wf[ie][t] insns

tests/tcg/aarch64/system/wfx.c line 79

Quoted 11 lines · Alex Bennée

This is based on a generated test case which I've since refined and cleaned up.

---

tests/tcg/aarch64/system/wfx.c new file +5 -0
+    /* Validate the timer fired and then disable for future tests */
+    if (!read_sysreg(cntv_ctl_el0) & 0x4) {
+        ml_printf("Time ISTATUS not set!\n");
+        return 1;
+    }

Should this be if (!(read_sysreg(cntv_ctl_el0) & 0x4)) ?

Jim