Skip to contentSkip to conversation

HWCAP_CPUID registers for aarch64

PATCH In review Alex Bennée 2 participants 10 messages Feb 5, 2019 – Feb 14, 2019

lore
Share & tools
lore
#0 lore raw

v2 cover HWCAP_CPUID registers for aarch64

Hi,

I've re-spun the cpuid patches with the changes suggested by Peter's review. The biggest change is the squashing of bits is now all data driven with ARMCPRegUserSpaceInfo being used to control how bits are altered for userspace presentation. This includes using glob matching to set whole bunches to RAZ.

The testcase isn't as comprehensive as it could be because you need a fairly new compiler (binutils) to emit all the various system register id's to test. I did look into upgrading debian-arm64-cross with Buster but I managed to find a bug in Debian's dependencies which rules out upgrading for now.

checkpatch is complaining about the _m macro I used to group together words in the masks I defined. I'm not sure adding the spaces makes it as readable though.

The following patches need review:
patch 0001/target arm relax permission checks for HWCAP_CPUI.patch patch 0002/target arm expose CPUID registers to userspace.patch patch 0003/target arm expose MPIDR_EL1 to userspace.patch patch 0004/target arm expose remaining CPUID registers as RA.patch patch 0006/tests tcg aarch64 userspace system register test.patch

Alex Bennée (6):
target/arm: relax permission checks for HWCAP_CPUID registers target/arm: expose CPUID registers to userspace target/arm: expose MPIDR_EL1 to userspace target/arm: expose remaining CPUID registers as RAZ linux-user/elfload: enable HWCAP_CPUID for AArch64 tests/tcg/aarch64: userspace system register test

 linux-user/elfload.c              |   1 +
 target/arm/cpu.h                  |  36 +++++++
 target/arm/helper.c               | 106 ++++++++++++++++--
 tests/tcg/aarch64/Makefile.target |   4 +-
 tests/tcg/aarch64/sysregs.c       | 172 ++++++++++++++++++++++++++++++
 5 files changed, 310 insertions(+), 9 deletions(-)
 create mode 100644 tests/tcg/aarch64/sysregs.c
Signature · 1 line
2.20.1
#1 lore raw

PATCH v2 1/6 target/arm: relax permission checks for HWCAP_CPUID registers

Although technically not visible to userspace the kernel does make them visible via a trap and emulate ABI. We provide a new permission mask (PL0U_R) which maps to PL0_R for CONFIG_USER builds and adjust the minimum permission check accordingly.

---
 target/arm/cpu.h    | 12 ++++++++++++
 target/arm/helper.c |  6 +++++-
 2 files changed, 17 insertions(+), 1 deletion(-)
target/arm/cpu.h +12 -0
index a68bcc9fed..1616632dcb 100644
@@ -2211,6 +2211,18 @@ static inline bool cptype_valid(int cptype)
 #define PL0_R (0x02 | PL1_R)
 #define PL0_W (0x01 | PL1_W)
 
+/*
+ * For user-mode some registers are accessible to EL0 via a kernel
+ * trap-and-emulate ABI. In this case we define the read permissions
+ * as actually being PL0_R. However some bits of any given register
+ * may still be masked.
+ */
+#ifdef CONFIG_USER_ONLY
+#define PL0U_R PL0_R
+#else
+#define PL0U_R PL1_R
+#endif
+
 #define PL3_RW (PL3_R | PL3_W)
 #define PL2_RW (PL2_R | PL2_W)
 #define PL1_RW (PL1_R | PL1_W)
target/arm/helper.c +5 -1
index d070879894..5857c0ba96 100644
@@ -6851,7 +6851,11 @@ void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
     if (r->state != ARM_CP_STATE_AA32) {
         int mask = 0;
         switch (r->opc1) {
-        case 0: case 1: case 2:
+        case 0:
+            /* min_EL EL1, but some accessible to EL0 via kernel ABI */
+            mask = PL0U_R | PL1_RW;
+            break;
+        case 1: case 2:
             /* min_EL EL1 */
             mask = PL1_RW;
             break;
Signature · 1 line
2.20.1
#2 lore raw

PATCH v2 2/6 target/arm: expose CPUID registers to userspace

In reply to Alex Bennée

A number of CPUID registers are exposed to userspace by modern Linux kernels thanks to the "ARM64 CPU Feature Registers" ABI. For QEMU's user-mode emulation we don't need to emulate the kernels trap but just return the value the trap would have done. To avoid too much #ifdef hackery we process ARMCPRegInfo with a new helper (modify_arm_cp_regs) before defining the registers. The modify routine is driven by a simple data structure which describes which bits are exported and which are fixed.

---
v4
- tweak commit message
- use PL0U_R instead of PL1U_R to be less confusing
- more CONFIG_USER logic for special cases
- mask a bunch of bits for some registers
v5
- use data driven modify_arm_cp_regs

---
 target/arm/cpu.h    | 21 ++++++++++++++++
 target/arm/helper.c | 59 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+)
target/arm/cpu.h +21 -0
index 1616632dcb..354df22102 100644
@@ -2449,6 +2449,27 @@ static inline void define_one_arm_cp_reg(ARMCPU *cpu, const ARMCPRegInfo *regs)
 }
 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp);
 
+/*
+ * Definition of an ARM co-processor register as viewed from
+ * userspace. This is used for presenting sanitised versions of
+ * registers to userspace when emulating the Linux AArch64 CPU
+ * ID/feature ABI (advertised as HWCAP_CPUID).
+ */
+typedef struct ARMCPRegUserSpaceInfo {
+    /* Name of register */
+    const char *name;
+
+    /* Only some bits are exported to user space */
+    uint64_t exported_bits;
+
+    /* Fixed bits are applied after the mask */
+    uint64_t fixed_bits;
+} ARMCPRegUserSpaceInfo;
+
+#define REGUSERINFO_SENTINEL { .name = NULL }
+
+void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods);
+
 /* CPWriteFn that can be used to implement writes-ignored behaviour */
 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
                          uint64_t value);
target/arm/helper.c +59 -0

77 lines not on this page · raw message

Signature · 1 line
2.20.1
#3 lore raw

PATCH v2 3/6 target/arm: expose MPIDR_EL1 to userspace

In reply to Alex Bennée

As this is a single register we could expose it with a simple ifdef but we use the existing modify_arm_cp_regs mechanism for consistency.

---
 target/arm/helper.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
target/arm/helper.c +14 -7
index f90754cc11..f2f868ff92 100644
@@ -3657,13 +3657,6 @@ static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
     return mpidr_read_val(env);
 }
 
-static const ARMCPRegInfo mpidr_cp_reginfo[] = {
-    { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
-      .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
-      .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
-    REGINFO_SENTINEL
-};
-
 static const ARMCPRegInfo lpae_cp_reginfo[] = {
     /* NOP AMAIR0/1 */
     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
@@ -6445,6 +6438,20 @@ void register_cp_regs_for_features(ARMCPU *cpu)
     }
 
     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
+        ARMCPRegInfo mpidr_cp_reginfo[] = {
+            { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
+              .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
+              .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
+            REGINFO_SENTINEL
+        };
+#ifdef CONFIG_USER_ONLY
+        ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
+            { .name = "MPIDR_EL1",
+              .fixed_bits = 0x0000000080000000 },
+            REGUSERINFO_SENTINEL
+        };
+        modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
+#endif
         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
     }
 
Signature · 1 line
2.20.1
#4 lore raw

PATCH v2 4/6 target/arm: expose remaining CPUID registers as RAZ

In reply to Alex Bennée

There are a whole bunch more registers in the CPUID space which are currently not used but are exposed as RAZ. To avoid too much duplication we expand ARMCPRegUserSpaceInfo to understand glob patterns so we only need one entry to tweak whole ranges of registers.

---
 target/arm/cpu.h    |  3 +++
 target/arm/helper.c | 26 +++++++++++++++++++++++---
 2 files changed, 26 insertions(+), 3 deletions(-)
target/arm/cpu.h +3 -0
index 354df22102..ae8ccc7dec 100644
@@ -2459,6 +2459,9 @@ typedef struct ARMCPRegUserSpaceInfo {
     /* Name of register */
     const char *name;
 
+    /* Is the name actually a glob pattern */
+    bool is_glob;
+
     /* Only some bits are exported to user space */
     uint64_t exported_bits;
 
target/arm/helper.c +23 -3
index f2f868ff92..e999da165b 100644
@@ -6103,19 +6103,27 @@ void register_cp_regs_for_features(ARMCPU *cpu)
               .fixed_bits    = 0x0000000000000011 },
             { .name = "ID_AA64PFR1_EL1",
               .exported_bits = 0x00000000000000f0 },
+            { .name = "ID_AA64PFR*_EL1_RESERVED",
+              .is_glob = true                     },
             { .name = "ID_AA64ZFR0_EL1"           },
             { .name = "ID_AA64MMFR0_EL1",
               .fixed_bits    = 0x00000000ff000000 },
             { .name = "ID_AA64MMFR1_EL1"          },
+            { .name = "ID_AA64MMFR*_EL1_RESERVED",
+              .is_glob = true                     },
             { .name = "ID_AA64DFR0_EL1",
               .fixed_bits    = 0x0000000000000006 },
             { .name = "ID_AA64DFR1_EL1"           },
-            { .name = "ID_AA64AFR0_EL1"           },
-            { .name = "ID_AA64AFR1_EL1"           },
+            { .name = "ID_AA64DFR*_EL1_RESERVED",
+              .is_glob = true                     },
+            { .name = "ID_AA64AFR*",
+              .is_glob = true                     },
             { .name = "ID_AA64ISAR0_EL1",
               .exported_bits = 0x00fffffff0fffff0 },
             { .name = "ID_AA64ISAR1_EL1",
               .exported_bits = 0x000000f0ffffffff },
+            { .name = "ID_AA64ISAR*_EL1_RESERVED",
+              .is_glob = true                     },
             REGUSERINFO_SENTINEL
         };
         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
@@ -7014,8 +7022,17 @@ void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods)
     ARMCPRegInfo *r;
 
     for (m = mods; m->name; m++) {
+        GPatternSpec *pat = NULL;
+        if (m->is_glob) {
+            pat = g_pattern_spec_new(m->name);
+        }
         for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
-            if (strcmp(r->name, m->name) == 0) {
+            if (pat && g_pattern_match_string(pat, r->name)) {
+                r->type = ARM_CP_CONST;
+                r->access = PL0U_R;
+                r->resetvalue = 0;
+                /* continue */
+            } else if (strcmp(r->name, m->name) == 0) {
                 r->type = ARM_CP_CONST;
                 r->access = PL0U_R;
                 r->resetvalue &= m->exported_bits;
@@ -7023,6 +7040,9 @@ void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods)
                 break;
             }
         }
+        if (pat) {
+            g_pattern_spec_free(pat);
+        }
     }
 }
 
Signature · 1 line
2.20.1
#5 lore raw

PATCH v2 5/6 linux-user/elfload: enable HWCAP_CPUID for AArch64

In reply to Alex Bennée

Userspace programs should (in theory) query the ELF HWCAP before probing these registers. Now we have implemented them all make it public.

---
 linux-user/elfload.c | 1 +
 1 file changed, 1 insertion(+)
linux-user/elfload.c +1 -0
index 775a36ccdd..3a50d587ff 100644
@@ -580,6 +580,7 @@ static uint32_t get_elf_hwcap(void)
 
     hwcaps |= ARM_HWCAP_A64_FP;
     hwcaps |= ARM_HWCAP_A64_ASIMD;
+    hwcaps |= ARM_HWCAP_A64_CPUID;
 
     /* probe for the extra features */
 #define GET_FEATURE_ID(feat, hwcap) \
Signature · 1 line
2.20.1
#6 lore raw

PATCH v2 6/6 tests/tcg/aarch64: userspace system register test

In reply to Alex Bennée

This tests a bunch of registers that the kernel allows userspace to read including the CPUID registers.

---
v4
- also test for extra bits that shouldn't be exposed
v5
- work around missing HWCAP_CPUID on older compilers
- add more registers to test and some aarch32 regs
- add more details commentary
- fix up the masks (add a helper to help keep track)
- add copyright header

---
 tests/tcg/aarch64/Makefile.target |   4 +-
 tests/tcg/aarch64/sysregs.c       | 172 ++++++++++++++++++++++++++++++
 2 files changed, 175 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/aarch64/sysregs.c
tests/tcg/aarch64/Makefile.target +3 -1
index 08c45b8470..fb40896e7b 100644
@@ -7,11 +7,13 @@ VPATH 		+= $(AARCH64_SRC)
 
 # we don't build any of the ARM tests
 AARCH64_TESTS=$(filter-out $(ARM_TESTS), $(TESTS))
-AARCH64_TESTS+=fcvt
+AARCH64_TESTS+=fcvt sysregs
 TESTS:=$(AARCH64_TESTS)
 
 fcvt: LDFLAGS+=-lm
 
+sysregs: CFLAGS+=-march=armv8.1-a+sve
+
 run-fcvt: fcvt
 	$(call run-test,$<,$(QEMU) $<, "$< on $(TARGET_NAME)")
 	$(call diff-out,$<,$(AARCH64_SRC)/fcvt.ref)
tests/tcg/aarch64/sysregs.c new file +172 -0

172 lines not on this page · raw message

Signature · 1 line
2.20.1
#7 lore raw
In reply to Alex Bennée
Quoted 7 lines · Alex Bennée

Hi,

I've re-spun the cpuid patches with the changes suggested by Peter's review. The biggest change is the squashing of bits is now all data driven with ARMCPRegUserSpaceInfo being used to control how bits are altered for userspace presentation. This includes using glob matching to set whole bunches to RAZ.

Ping (+ CC'd which I missed on the submission)

Quoted 6 lines

The testcase isn't as comprehensive as it could be because you need a fairly new compiler (binutils) to emit all the various system register id's to test. I did look into upgrading debian-arm64-cross with Buster but I managed to find a bug in Debian's dependencies which rules out upgrading for now.

We have a newer compiler so I can add more on the next re-spin, reviews pending.

Quoted 21 lines

checkpatch is complaining about the _m macro I used to group together words in the masks I defined. I'm not sure adding the spaces makes it as readable though.

The following patches need review:
patch 0001/target arm relax permission checks for HWCAP_CPUI.patch patch 0002/target arm expose CPUID registers to userspace.patch patch 0003/target arm expose MPIDR_EL1 to userspace.patch patch 0004/target arm expose remaining CPUID registers as RA.patch patch 0006/tests tcg aarch64 userspace system register test.patch

Alex Bennée (6):
target/arm: relax permission checks for HWCAP_CPUID registers target/arm: expose CPUID registers to userspace target/arm: expose MPIDR_EL1 to userspace target/arm: expose remaining CPUID registers as RAZ linux-user/elfload: enable HWCAP_CPUID for AArch64 tests/tcg/aarch64: userspace system register test

Quoted 7 lines
 linux-user/elfload.c              |   1 +
 target/arm/cpu.h                  |  36 +++++++
 target/arm/helper.c               | 106 ++++++++++++++++--
 tests/tcg/aarch64/Makefile.target |   4 +-
 tests/tcg/aarch64/sysregs.c       | 172 ++++++++++++++++++++++++++++++
 5 files changed, 310 insertions(+), 9 deletions(-)
 create mode 100644 tests/tcg/aarch64/sysregs.c

--
Alex Bennée

#8 lore raw
In reply to Alex Bennée
Quoted 19 lines · Alex Bennée

Hi,

I've re-spun the cpuid patches with the changes suggested by Peter's review. The biggest change is the squashing of bits is now all data driven with ARMCPRegUserSpaceInfo being used to control how bits are altered for userspace presentation. This includes using glob matching to set whole bunches to RAZ.

The testcase isn't as comprehensive as it could be because you need a fairly new compiler (binutils) to emit all the various system register id's to test. I did look into upgrading debian-arm64-cross with Buster but I managed to find a bug in Debian's dependencies which rules out upgrading for now.

checkpatch is complaining about the _m macro I used to group together words in the masks I defined. I'm not sure adding the spaces makes it as readable though.

Quoted 6 lines

The following patches need review:
patch 0001/target arm relax permission checks for HWCAP_CPUI.patch patch 0002/target arm expose CPUID registers to userspace.patch patch 0003/target arm expose MPIDR_EL1 to userspace.patch patch 0004/target arm expose remaining CPUID registers as RA.patch patch 0006/tests tcg aarch64 userspace system register test.patch

I'll take 1-5 into target-arm.next, but 6 breaks 'make check-tcg' for me:

BUILD aarch64 guest-tests with aarch64-linux-gnu-gcc
cc1: error: invalid feature modifier in ‘-march=armv8.1-a+sve’

I imagine you're assuming a newer binutils than Ubuntu ships. You should probably go for just encoding the insns rather than using their names -- binutils should accept "S<op0>_<op1>_<Cn>_<Cm>_<op2>" as a register name for mrs and msr insn.

It would also be nice to test some of the sysreg cases which go through your "glob the register name" code path -- I think at the moment you are only testing the ones which use the specific-match case ? (Ideally we'd loop through and test that we could read everything in the defined-to-be-exposed encoding range, though that would require some slightly tedious preprocessor macro effort.)

thanks
-- PMM

#9 lore raw
Quoted 30 lines · Peter Maydell
Quoted 19 lines · Alex Bennée

Hi,

I've re-spun the cpuid patches with the changes suggested by Peter's review. The biggest change is the squashing of bits is now all data driven with ARMCPRegUserSpaceInfo being used to control how bits are altered for userspace presentation. This includes using glob matching to set whole bunches to RAZ.

The testcase isn't as comprehensive as it could be because you need a fairly new compiler (binutils) to emit all the various system register id's to test. I did look into upgrading debian-arm64-cross with Buster but I managed to find a bug in Debian's dependencies which rules out upgrading for now.

checkpatch is complaining about the _m macro I used to group together words in the masks I defined. I'm not sure adding the spaces makes it as readable though.

Quoted 6 lines

The following patches need review:
patch 0001/target arm relax permission checks for HWCAP_CPUI.patch patch 0002/target arm expose CPUID registers to userspace.patch patch 0003/target arm expose MPIDR_EL1 to userspace.patch patch 0004/target arm expose remaining CPUID registers as RA.patch patch 0006/tests tcg aarch64 userspace system register test.patch

I'll take 1-5 into target-arm.next, but 6 breaks 'make check-tcg' for me:

Quoted 7 lines

BUILD aarch64 guest-tests with aarch64-linux-gnu-gcc
cc1: error: invalid feature modifier in ‘-march=armv8.1-a+sve’

I imagine you're assuming a newer binutils than Ubuntu ships. You should probably go for just encoding the insns rather than using their names -- binutils should accept "S<op0>_<op1>_<Cn>_<Cm>_<op2>" as a register name for mrs and msr insn.

Yeah I'm now on buster so have a newer gcc:

aarch64-linux-gnu-gcc (Debian 8.2.0-11) 8.2.0

But I'm surprised that the pauth test cases worked on your system. However they should all work with the recent docker update which you merged earlier this week.

Quoted 7 lines

It would also be nice to test some of the sysreg cases which go through your "glob the register name" code path -- I think at the moment you are only testing the ones which use the specific-match case ? (Ideally we'd loop through and test that we could read everything in the defined-to-be-exposed encoding range, though that would require some slightly tedious preprocessor macro effort.)

Yeah the main reason I didn't push to hard to do it but I guess we should for completeness sake. It's times like this I wish C's macros weren't the pale imitations of what you can do with lisp (or rust ;-).

--
Alex Bennée