Skip to contentSkip to conversation

target/arm: Complete ARMv8.3-PAuth linux-user

PATCH Applied Richard Henderson 4 participants 15 messages Jan 25, 2019 – Feb 1, 2019

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover target/arm: Complete ARMv8.3-PAuth linux-user

(1) Fix a bug I introduced at the last moment in the last patch set -- enable pac keys during reset, not before.
(2) Add the HWCAP bits.
(3) Add the new prctl
(4) Add a smoke test so that (1) doesn't happen again.

r~

Richard Henderson (6):
target/arm: Always enable pac keys for user-only
aarch64-linux-user: Update HWCAP bits from linux 5.0-rc1
aarch64-linux-user: Enable HWCAP bits for PAuth
linux-user: Initialize aarch64 pac keys
linux-user: Implement PR_PAC_RESET_KEYS tests/tcg/aarch64: Add pauth smoke tests

 linux-user/aarch64/target_syscall.h |  9 +++++
 linux-user/aarch64/cpu_loop.c       | 31 ++++++++++++++-
 linux-user/elfload.c                | 10 +++++
 linux-user/syscall.c                | 33 ++++++++++++++++
 target/arm/cpu.c                    |  3 ++
 target/arm/cpu64.c                  | 60 -----------------------------
 tests/tcg/aarch64/pauth-1.c         | 23 +++++++++++
 tests/tcg/aarch64/Makefile.target   |  7 +++-
 8 files changed, 113 insertions(+), 63 deletions(-)
 create mode 100644 tests/tcg/aarch64/pauth-1.c
Signature · 1 line
2.17.2
#1 lore raw

PATCH 1/6 target/arm: Always enable pac keys for user-only

Drop the pac properties. This approach cannot work as written because the properties are applied before arm_cpu_reset, which zeros SCTLR_EL1 (amongst everything else).

We can re-introduce the properties if they turn out to be useful. But since linux 5.0 enables all of the keys, they may not be.

Fixes 1ae9cfbd470
---
 target/arm/cpu.c   |  3 +++
 target/arm/cpu64.c | 60 ----------------------------------------------
 2 files changed, 3 insertions(+), 60 deletions(-)
target/arm/cpu.c +3 -0
index 7e1f3dd637..1e79b97a9c 100644
@@ -162,6 +162,9 @@ static void arm_cpu_reset(CPUState *s)
         env->pstate = PSTATE_MODE_EL0t;
         /* Userspace expects access to DC ZVA, CTL_EL0 and the cache ops */
         env->cp15.sctlr_el[1] |= SCTLR_UCT | SCTLR_UCI | SCTLR_DZE;
+        /* Enable all PAC keys.  */
+        env->cp15.sctlr_el[1] |= (SCTLR_EnIA | SCTLR_EnIB |
+                                  SCTLR_EnDA | SCTLR_EnDB);
         /* Enable all PAC instructions */
         env->cp15.hcr_el2 |= HCR_API;
         env->cp15.scr_el3 |= SCR_API;
target/arm/cpu64.c +0 -60

72 lines not on this page · raw message

Signature · 1 line
2.17.2
#2 lore raw

PATCH 2/6 aarch64-linux-user: Update HWCAP bits from linux 5.0-rc1

In reply to Richard Henderson
---
 linux-user/elfload.c | 9 +++++++++
 1 file changed, 9 insertions(+)
linux-user/elfload.c +9 -0
index 4cff9e1a31..3c7a7c2836 100644
@@ -560,6 +560,15 @@ enum {
     ARM_HWCAP_A64_ASIMDDP       = 1 << 20,
     ARM_HWCAP_A64_SHA512        = 1 << 21,
     ARM_HWCAP_A64_SVE           = 1 << 22,
+    ARM_HWCAP_A64_ASIMDFHM      = 1 << 23,
+    ARM_HWCAP_A64_DIT           = 1 << 24,
+    ARM_HWCAP_A64_USCAT         = 1 << 25,
+    ARM_HWCAP_A64_ILRCPC        = 1 << 26,
+    ARM_HWCAP_A64_FLAGM         = 1 << 27,
+    ARM_HWCAP_A64_SSBS          = 1 << 28,
+    ARM_HWCAP_A64_SB            = 1 << 29,
+    ARM_HWCAP_A64_PACA          = 1 << 30,
+    ARM_HWCAP_A64_PACG          = 1UL << 31,
 };
 
 #define ELF_HWCAP get_elf_hwcap()
Signature · 1 line
2.17.2
Reviewed-by on v1 2/6
#3 lore raw

Re: [Qemu-devel] [PATCH 2/6] aarch64-linux-user: Update HWCAP bits from linux 5.0-rc1

Quoted 16 lines · Richard Henderson
---
 linux-user/elfload.c | 9 +++++++++
 1 file changed, 9 insertions(+)
linux-user/elfload.c +2 -0
index 4cff9e1a31..3c7a7c2836 100644
@@ -560,6 +560,15 @@ enum {
     ARM_HWCAP_A64_ASIMDDP       = 1 << 20,
     ARM_HWCAP_A64_SHA512        = 1 << 21,
     ARM_HWCAP_A64_SVE           = 1 << 22,
+    ARM_HWCAP_A64_ASIMDFHM      = 1 << 23,
+    ARM_HWCAP_A64_DIT           = 1 << 24,

linux-user/elfload.c enum { · line 574

Quoted 10 lines
linux-user/elfload.c +7 -0
+    ARM_HWCAP_A64_USCAT         = 1 << 25,
+    ARM_HWCAP_A64_ILRCPC        = 1 << 26,
+    ARM_HWCAP_A64_FLAGM         = 1 << 27,
+    ARM_HWCAP_A64_SSBS          = 1 << 28,
+    ARM_HWCAP_A64_SB            = 1 << 29,
+    ARM_HWCAP_A64_PACA          = 1 << 30,
+    ARM_HWCAP_A64_PACG          = 1UL << 31,
 };
 
 #define ELF_HWCAP get_elf_hwcap()
#4 lore raw

PATCH 3/6 aarch64-linux-user: Enable HWCAP bits for PAuth

In reply to Richard Henderson
---
 linux-user/elfload.c | 1 +
 1 file changed, 1 insertion(+)
linux-user/elfload.c +1 -0
index 3c7a7c2836..775a36ccdd 100644
@@ -600,6 +600,7 @@ static uint32_t get_elf_hwcap(void)
     GET_FEATURE_ID(aa64_dp, ARM_HWCAP_A64_ASIMDDP);
     GET_FEATURE_ID(aa64_fcma, ARM_HWCAP_A64_FCMA);
     GET_FEATURE_ID(aa64_sve, ARM_HWCAP_A64_SVE);
+    GET_FEATURE_ID(aa64_pauth, ARM_HWCAP_A64_PACA | ARM_HWCAP_A64_PACG);
 
 #undef GET_FEATURE_ID
 
Signature · 1 line
2.17.2
#5 lore raw

PATCH 4/6 linux-user: Initialize aarch64 pac keys

In reply to Richard Henderson

Initialize the keys to a non-zero value on process start.

---
 linux-user/aarch64/target_syscall.h |  2 ++
 linux-user/aarch64/cpu_loop.c       | 31 +++++++++++++++++++++++++++--
 2 files changed, 31 insertions(+), 2 deletions(-)
linux-user/aarch64/target_syscall.h +2 -0
index 205265e619..937fd7989e 100644
@@ -22,4 +22,6 @@ struct target_pt_regs {
 #define TARGET_PR_SVE_SET_VL  50
 #define TARGET_PR_SVE_GET_VL  51
 
+void arm_init_pauth_key(ARMPACKey *key);
+
 #endif /* AARCH64_TARGET_SYSCALL_H */
linux-user/aarch64/cpu_loop.c +29 -2
index 65d815f030..d75fd9d3e2 100644
@@ -147,10 +147,29 @@ void cpu_loop(CPUARMState *env)
     }
 }
 
+static uint64_t arm_rand64(void)
+{
+    int shift = 64 - clz64(RAND_MAX);
+    int i, n = 64 / shift + (64 % shift != 0);
+    uint64_t ret = 0;
+
+    for (i = 0; i < n; i++) {
+        ret = (ret << shift) | rand();
+    }
+    return ret;
+}
+
+void arm_init_pauth_key(ARMPACKey *key)
+{
+    key->lo = arm_rand64();
+    key->hi = arm_rand64();
+}
+
 void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
 {
-    CPUState *cpu = ENV_GET_CPU(env);
-    TaskState *ts = cpu->opaque;
+    ARMCPU *cpu = arm_env_get_cpu(env);
+    CPUState *cs = CPU(cpu);
+    TaskState *ts = cs->opaque;
     struct image_info *info = ts->info;
     int i;
 
@@ -172,6 +191,14 @@ void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
     }
 #endif
 
+    if (cpu_isar_feature(aa64_pauth, cpu)) {
+        arm_init_pauth_key(&env->apia_key);
+        arm_init_pauth_key(&env->apib_key);
+        arm_init_pauth_key(&env->apda_key);
+        arm_init_pauth_key(&env->apdb_key);
+        arm_init_pauth_key(&env->apga_key);
+    }
+
     ts->stack_base = info->start_stack;
     ts->heap_base = info->brk;
     /* This will be filled in on the first SYS_HEAPINFO call.  */
Signature · 1 line
2.17.2
#6 lore raw

Re: [Qemu-devel] [PATCH 4/6] linux-user: Initialize aarch64 pac keys

Quoted 6 lines · Richard Henderson

Initialize the keys to a non-zero value on process start.

linux-user/aarch64/cpu_loop.c +1 -0
+static uint64_t arm_rand64(void)

linux-user/aarch64/cpu_loop.c void cpu_loop(CPUARMState *env) · line 160

Quoted 10 lines
linux-user/aarch64/cpu_loop.c +10 -0
+{
+    int shift = 64 - clz64(RAND_MAX);
+    int i, n = 64 / shift + (64 % shift != 0);
+    uint64_t ret = 0;
+
+    for (i = 0; i < n; i++) {
+        ret = (ret << shift) | rand();
+    }
+    return ret;
+}

I'm not a huge fan of the use of rand() here, but it's what we're using to initialize AT_RANDOM in linux-user, so I guess it's OK here. At some point we should investigate whether there's something better we are guaranteed to have available, I suppose. (Coverity gripes about use of rand().)

thanks
-- PMM

#7 lore raw

Re: [Qemu-devel] [PATCH 4/6] linux-user: Initialize aarch64 pac keys

Quoted 16 lines · Peter Maydell
Quoted 14 lines · Richard Henderson

Initialize the keys to a non-zero value on process start.

linux-user/aarch64/cpu_loop.c +9 -0
+static uint64_t arm_rand64(void)
+{
+    int shift = 64 - clz64(RAND_MAX);
+    int i, n = 64 / shift + (64 % shift != 0);
+    uint64_t ret = 0;
+
+    for (i = 0; i < n; i++) {
+        ret = (ret << shift) | rand();
+    }
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+    return ret;
+}

I'm not a huge fan of the use of rand() here, but it's what we're using to initialize AT_RANDOM in linux-user, so I guess it's OK here. At some point we should investigate whether there's something better we are guaranteed to have available, I suppose. (Coverity gripes about use of rand().)

I considered implementing a qemu version of getentropy, but then I saw our "-seed" option and thought perhaps repeatability is more important for qemu.

r~

#8 lore raw

PATCH 5/6 linux-user: Implement PR_PAC_RESET_KEYS

In reply to Richard Henderson
---
 linux-user/aarch64/target_syscall.h |  7 ++++++
 linux-user/syscall.c                | 33 +++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
linux-user/aarch64/target_syscall.h +7 -0
index 937fd7989e..b595e5da82 100644
@@ -22,6 +22,13 @@ struct target_pt_regs {
 #define TARGET_PR_SVE_SET_VL  50
 #define TARGET_PR_SVE_GET_VL  51
 
+#define TARGET_PR_PAC_RESET_KEYS 54
+# define TARGET_PR_PAC_APIAKEY   (1 << 0)
+# define TARGET_PR_PAC_APIBKEY   (1 << 1)
+# define TARGET_PR_PAC_APDAKEY   (1 << 2)
+# define TARGET_PR_PAC_APDBKEY   (1 << 3)
+# define TARGET_PR_PAC_APGAKEY   (1 << 4)
+
 void arm_init_pauth_key(ARMPACKey *key);
 
 #endif /* AARCH64_TARGET_SYSCALL_H */
linux-user/syscall.c +33 -0
index b5786d4fc1..3e2949aa2f 100644
@@ -9691,6 +9691,39 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
                 }
             }
             return ret;
+        case TARGET_PR_PAC_RESET_KEYS:
+            {
+                CPUARMState *env = cpu_env;
+                ARMCPU *cpu = arm_env_get_cpu(env);
+
+                if (cpu_isar_feature(aa64_pauth, cpu)) {
+                    int all = (TARGET_PR_PAC_APIAKEY | TARGET_PR_PAC_APIBKEY |
+                               TARGET_PR_PAC_APDAKEY | TARGET_PR_PAC_APDBKEY |
+                               TARGET_PR_PAC_APGAKEY);
+                    if (arg2 == 0) {
+                        arg2 = all;
+                    } else if (arg2 & ~all) {
+                        return -TARGET_EINVAL;
+                    }
+                    if (arg2 & TARGET_PR_PAC_APIAKEY) {
+                        arm_init_pauth_key(&env->apia_key);
+                    }
+                    if (arg2 & TARGET_PR_PAC_APIBKEY) {
+                        arm_init_pauth_key(&env->apib_key);
+                    }
+                    if (arg2 & TARGET_PR_PAC_APDAKEY) {
+                        arm_init_pauth_key(&env->apda_key);
+                    }
+                    if (arg2 & TARGET_PR_PAC_APDBKEY) {
+                        arm_init_pauth_key(&env->apdb_key);
+                    }
+                    if (arg2 & TARGET_PR_PAC_APGAKEY) {
+                        arm_init_pauth_key(&env->apga_key);
+                    }
+                    return 0;
+                }
+            }
+            return -TARGET_EINVAL;
 #endif /* AARCH64 */
         case PR_GET_SECCOMP:
         case PR_SET_SECCOMP:
Signature · 1 line
2.17.2
Reviewed-by on v1 5/6
#9 lore raw

Re: [Qemu-devel] [PATCH 5/6] linux-user: Implement PR_PAC_RESET_KEYS

Quoted 8 lines · Richard Henderson
---
 linux-user/aarch64/target_syscall.h |  7 ++++++
 linux-user/syscall.c                | 33 +++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 9697

Quoted 7 lines
linux-user/syscall.c +4 -0
@@ -9691,6 +9691,39 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
                 }
             }
             return ret;
+        case TARGET_PR_PAC_RESET_KEYS:
+            {
+                CPUARMState *env = cpu_env;
+                ARMCPU *cpu = arm_env_get_cpu(env);

The kernel implementation of this returns EINVAL if any of arg3/arg4/arg5 are non-zero.

Otherwise

thanks
-- PMM

#10 lore raw

PATCH 6/6 tests/tcg/aarch64: Add pauth smoke tests

In reply to Richard Henderson
---
 tests/tcg/aarch64/pauth-1.c       | 23 +++++++++++++++++++++++
 tests/tcg/aarch64/Makefile.target |  7 ++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/aarch64/pauth-1.c
tests/tcg/aarch64/pauth-1.c new file +23 -0
new file mode 100644
index 0000000000..9bd8d28ede
@@ -0,0 +1,23 @@
+#include <assert.h>
+#include <sys/prctl.h>
+
+asm(".arch armv8.4-a");
+
+#ifndef PR_PAC_RESET_KEYS
+#define PR_PAC_RESET_KEYS  54
+#define PR_PAC_APDAKEY     (1 << 2)
+#endif
+
+int main()
+{
+    int x;
+    void *p0 = &x, *p1, *p2;
+
+    asm volatile("pacdza %0" : "=r"(p1) : "0"(p0));
+    prctl(PR_PAC_RESET_KEYS, PR_PAC_APDAKEY);
+    asm volatile("pacdza %0" : "=r"(p2) : "0"(p0));
+
+    assert(p1 != p0);
+    assert(p1 != p2);
+    return 0;
+}
tests/tcg/aarch64/Makefile.target +6 -1
index 08c45b8470..e80d07276c 100644
@@ -8,10 +8,15 @@ VPATH 		+= $(AARCH64_SRC)
 # we don't build any of the ARM tests
 AARCH64_TESTS=$(filter-out $(ARM_TESTS), $(TESTS))
 AARCH64_TESTS+=fcvt
-TESTS:=$(AARCH64_TESTS)
 
 fcvt: LDFLAGS+=-lm
 
 run-fcvt: fcvt
 	$(call run-test,$<,$(QEMU) $<, "$< on $(TARGET_NAME)")
 	$(call diff-out,$<,$(AARCH64_SRC)/fcvt.ref)
+
+AARCH64_TESTS += pauth-1
+pauth-%: CFLAGS += -O -g
+run-pauth-%: QEMU += -cpu max
+
+TESTS:=$(AARCH64_TESTS)
Signature · 1 line
2.17.2
Reviewed-by on v1 6/6
#11 lore raw

Re: [Qemu-devel] [PATCH 6/6] tests/tcg/aarch64: Add pauth smoke tests

Quoted 44 lines · Richard Henderson
---
 tests/tcg/aarch64/pauth-1.c       | 23 +++++++++++++++++++++++
 tests/tcg/aarch64/Makefile.target |  7 ++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/aarch64/pauth-1.c
tests/tcg/aarch64/pauth-1.c new file +23 -0
new file mode 100644
index 0000000000..9bd8d28ede
@@ -0,0 +1,23 @@
+#include <assert.h>
+#include <sys/prctl.h>
+
+asm(".arch armv8.4-a");
+
+#ifndef PR_PAC_RESET_KEYS
+#define PR_PAC_RESET_KEYS  54
+#define PR_PAC_APDAKEY     (1 << 2)
+#endif
+
+int main()
+{
+    int x;
+    void *p0 = &x, *p1, *p2;
+
+    asm volatile("pacdza %0" : "=r"(p1) : "0"(p0));
+    prctl(PR_PAC_RESET_KEYS, PR_PAC_APDAKEY);
+    asm volatile("pacdza %0" : "=r"(p2) : "0"(p0));
+
+    assert(p1 != p0);
+    assert(p1 != p2);
+    return 0;
+}
tests/tcg/aarch64/Makefile.target +0 -0
index 08c45b8470..e80d07276c 100644
@@ -8,10 +8,15 @@ VPATH 		+= $(AARCH64_SRC)
 # we don't build any of the ARM tests
 AARCH64_TESTS=$(filter-out $(ARM_TESTS), $(TESTS))
 AARCH64_TESTS+=fcvt

tests/tcg/aarch64/Makefile.target VPATH += $(AARCH64_SRC) · line 19

Quoted 10 lines
tests/tcg/aarch64/Makefile.target +3 -1
-TESTS:=$(AARCH64_TESTS)
 
 fcvt: LDFLAGS+=-lm
 
 run-fcvt: fcvt
 	$(call run-test,$<,$(QEMU) $<, "$< on $(TARGET_NAME)")
 	$(call diff-out,$<,$(AARCH64_SRC)/fcvt.ref)
+
+AARCH64_TESTS += pauth-1
+pauth-%: CFLAGS += -O -g

We build all tests with:

CFLAGS+=-Wall -O0 -g -fno-strict-aliasing

mainly because the first thing you want to do when they fail is run them through gdb to see what went wrong. Do you actually need optimisation on for the build to work? Everything else looks good though:

tests/tcg/aarch64/Makefile.target VPATH += $(AARCH64_SRC) · line 22

Quoted 3 lines
tests/tcg/aarch64/Makefile.target +3 -0
+run-pauth-%: QEMU += -cpu max
+
+TESTS:=$(AARCH64_TESTS)

--
Alex Bennée

#12 lore raw

Re: [Qemu-devel] [PATCH 6/6] tests/tcg/aarch64: Add pauth smoke tests

Quoted 7 lines · Alex Bennée

We build all tests with:

CFLAGS+=-Wall -O0 -g -fno-strict-aliasing

mainly because the first thing you want to do when they fail is run them through gdb to see what went wrong. Do you actually need optimisation on for the build to work? Everything else looks good though:

I needed optimization for the MemTag test to work, and I copied that. I'll remove it.

r~

#13 lore raw

PATCH 1/1 target/arm: Fix validation of 32-bit address spaces for aa32

In reply to Richard Henderson

When tsz == 0, aarch32 selects the address space via exclusion, and there are no "top_bits" remaining that require validation.

---
 target/arm/helper.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
target/arm/helper.c +13 -6
index 92666e5208..e24689f767 100644
@@ -10447,7 +10447,7 @@ static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
     uint64_t ttbr;
     hwaddr descaddr, indexmask, indexmask_grainsize;
     uint32_t tableattrs;
-    target_ulong page_size, top_bits;
+    target_ulong page_size;
     uint32_t attrs;
     int32_t stride;
     int addrsize, inputsize;
@@ -10487,12 +10487,19 @@ static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
      * We determined the region when collecting the parameters, but we
      * have not yet validated that the address is valid for the region.
      * Extract the top bits and verify that they all match select.
+     *
+     * For aa32, if inputsize == addrsize, then we have selected the
+     * region by exclusion in aa32_va_parameters and there is no more
+     * validation to do here.
      */
-    top_bits = sextract64(address, inputsize, addrsize - inputsize);
-    if (-top_bits != param.select || (param.select && !ttbr1_valid)) {
-        /* In the gap between the two regions, this is a Translation fault */
-        fault_type = ARMFault_Translation;
-        goto do_fault;
+    if (inputsize < addrsize) {
+        target_ulong top_bits = sextract64(address, inputsize,
+                                           addrsize - inputsize);
+        if (-top_bits != param.select || (param.select && !ttbr1_valid)) {
+            /* The gap between the two regions is a Translation fault */
+            fault_type = ARMFault_Translation;
+            goto do_fault;
+        }
     }
 
     if (param.using64k) {
Signature · 1 line
2.17.1
#14 lore raw
In reply to Richard Henderson
Quoted 11 lines · Richard Henderson

(1) Fix a bug I introduced at the last moment in the last patch set -- enable pac keys during reset, not before.
(2) Add the HWCAP bits.
(3) Add the new prctl
(4) Add a smoke test so that (1) doesn't happen again.

r~

Quoted 7 lines

Richard Henderson (6):
target/arm: Always enable pac keys for user-only
aarch64-linux-user: Update HWCAP bits from linux 5.0-rc1
aarch64-linux-user: Enable HWCAP bits for PAuth
linux-user: Initialize aarch64 pac keys
linux-user: Implement PR_PAC_RESET_KEYS tests/tcg/aarch64: Add pauth smoke tests

Applied patches 1-4 to target-arm.next (5 has a minor nit and 6 depends on 5).

thanks
-- PMM