22c213
From d8871ae2842531130c9b333e7c06a6a5d1561286 Mon Sep 17 00:00:00 2001
22c213
From: Andrew Jones <drjones@redhat.com>
22c213
Date: Fri, 24 Jan 2020 09:14:34 +0100
22c213
Subject: [PATCH 001/116] target/arm/arch_dump: Add SVE notes
22c213
22c213
RH-Author: Andrew Jones <drjones@redhat.com>
22c213
Message-id: <20200124091434.15021-2-drjones@redhat.com>
22c213
Patchwork-id: 93443
22c213
O-Subject: [RHEL-AV-8.2.0 qemu-kvm PATCH 1/1] target/arm/arch_dump: Add SVE notes
22c213
Bugzilla: 1725084
22c213
RH-Acked-by: Auger Eric <eric.auger@redhat.com>
22c213
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
22c213
RH-Acked-by: Gavin Shan <gshan@redhat.com>
22c213
22c213
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1725084
22c213
22c213
Author: Andrew Jones <drjones@redhat.com>
22c213
Date:   Thu, 23 Jan 2020 15:22:40 +0000
22c213
22c213
    target/arm/arch_dump: Add SVE notes
22c213
22c213
    When dumping a guest with dump-guest-memory also dump the SVE
22c213
    registers if they are in use.
22c213
22c213
    Signed-off-by: Andrew Jones <drjones@redhat.com>
22c213
    Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
22c213
    Message-id: 20200120101832.18781-1-drjones@redhat.com
22c213
    [PMM: fixed checkpatch nits]
22c213
    Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
22c213
22c213
(cherry picked from commit 538baab245ca881e6a6ff720b5133f3ad1fcaafc)
22c213
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
22c213
---
22c213
 include/elf.h          |   1 +
22c213
 target/arm/arch_dump.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++++-
22c213
 target/arm/cpu.h       |  25 ++++++++++
22c213
 target/arm/kvm64.c     |  24 ----------
22c213
 4 files changed, 148 insertions(+), 26 deletions(-)
22c213
22c213
diff --git a/include/elf.h b/include/elf.h
22c213
index 3501e0c..8fbfe60 100644
22c213
--- a/include/elf.h
22c213
+++ b/include/elf.h
22c213
@@ -1650,6 +1650,7 @@ typedef struct elf64_shdr {
22c213
 #define NT_ARM_HW_BREAK 0x402           /* ARM hardware breakpoint registers */
22c213
 #define NT_ARM_HW_WATCH 0x403           /* ARM hardware watchpoint registers */
22c213
 #define NT_ARM_SYSTEM_CALL      0x404   /* ARM system call number */
22c213
+#define NT_ARM_SVE      0x405           /* ARM Scalable Vector Extension regs */
22c213
 
22c213
 /*
22c213
  * Physical entry point into the kernel.
22c213
diff --git a/target/arm/arch_dump.c b/target/arm/arch_dump.c
22c213
index 26a2c09..2345dec 100644
22c213
--- a/target/arm/arch_dump.c
22c213
+++ b/target/arm/arch_dump.c
22c213
@@ -62,12 +62,23 @@ struct aarch64_user_vfp_state {
22c213
 
22c213
 QEMU_BUILD_BUG_ON(sizeof(struct aarch64_user_vfp_state) != 528);
22c213
 
22c213
+/* struct user_sve_header from arch/arm64/include/uapi/asm/ptrace.h */
22c213
+struct aarch64_user_sve_header {
22c213
+    uint32_t size;
22c213
+    uint32_t max_size;
22c213
+    uint16_t vl;
22c213
+    uint16_t max_vl;
22c213
+    uint16_t flags;
22c213
+    uint16_t reserved;
22c213
+} QEMU_PACKED;
22c213
+
22c213
 struct aarch64_note {
22c213
     Elf64_Nhdr hdr;
22c213
     char name[8]; /* align_up(sizeof("CORE"), 4) */
22c213
     union {
22c213
         struct aarch64_elf_prstatus prstatus;
22c213
         struct aarch64_user_vfp_state vfp;
22c213
+        struct aarch64_user_sve_header sve;
22c213
     };
22c213
 } QEMU_PACKED;
22c213
 
22c213
@@ -76,6 +87,8 @@ struct aarch64_note {
22c213
             (AARCH64_NOTE_HEADER_SIZE + sizeof(struct aarch64_elf_prstatus))
22c213
 #define AARCH64_PRFPREG_NOTE_SIZE \
22c213
             (AARCH64_NOTE_HEADER_SIZE + sizeof(struct aarch64_user_vfp_state))
22c213
+#define AARCH64_SVE_NOTE_SIZE(env) \
22c213
+            (AARCH64_NOTE_HEADER_SIZE + sve_size(env))
22c213
 
22c213
 static void aarch64_note_init(struct aarch64_note *note, DumpState *s,
22c213
                               const char *name, Elf64_Word namesz,
22c213
@@ -128,11 +141,102 @@ static int aarch64_write_elf64_prfpreg(WriteCoreDumpFunction f,
22c213
     return 0;
22c213
 }
22c213
 
22c213
+#ifdef TARGET_AARCH64
22c213
+static off_t sve_zreg_offset(uint32_t vq, int n)
22c213
+{
22c213
+    off_t off = sizeof(struct aarch64_user_sve_header);
22c213
+    return ROUND_UP(off, 16) + vq * 16 * n;
22c213
+}
22c213
+
22c213
+static off_t sve_preg_offset(uint32_t vq, int n)
22c213
+{
22c213
+    return sve_zreg_offset(vq, 32) + vq * 16 / 8 * n;
22c213
+}
22c213
+
22c213
+static off_t sve_fpsr_offset(uint32_t vq)
22c213
+{
22c213
+    off_t off = sve_preg_offset(vq, 17);
22c213
+    return ROUND_UP(off, 16);
22c213
+}
22c213
+
22c213
+static off_t sve_fpcr_offset(uint32_t vq)
22c213
+{
22c213
+    return sve_fpsr_offset(vq) + sizeof(uint32_t);
22c213
+}
22c213
+
22c213
+static uint32_t sve_current_vq(CPUARMState *env)
22c213
+{
22c213
+    return sve_zcr_len_for_el(env, arm_current_el(env)) + 1;
22c213
+}
22c213
+
22c213
+static size_t sve_size_vq(uint32_t vq)
22c213
+{
22c213
+    off_t off = sve_fpcr_offset(vq) + sizeof(uint32_t);
22c213
+    return ROUND_UP(off, 16);
22c213
+}
22c213
+
22c213
+static size_t sve_size(CPUARMState *env)
22c213
+{
22c213
+    return sve_size_vq(sve_current_vq(env));
22c213
+}
22c213
+
22c213
+static int aarch64_write_elf64_sve(WriteCoreDumpFunction f,
22c213
+                                   CPUARMState *env, int cpuid,
22c213
+                                   DumpState *s)
22c213
+{
22c213
+    struct aarch64_note *note;
22c213
+    ARMCPU *cpu = env_archcpu(env);
22c213
+    uint32_t vq = sve_current_vq(env);
22c213
+    uint64_t tmp[ARM_MAX_VQ * 2], *r;
22c213
+    uint32_t fpr;
22c213
+    uint8_t *buf;
22c213
+    int ret, i;
22c213
+
22c213
+    note = g_malloc0(AARCH64_SVE_NOTE_SIZE(env));
22c213
+    buf = (uint8_t *)&note->sve;
22c213
+
22c213
+    aarch64_note_init(note, s, "LINUX", 6, NT_ARM_SVE, sve_size_vq(vq));
22c213
+
22c213
+    note->sve.size = cpu_to_dump32(s, sve_size_vq(vq));
22c213
+    note->sve.max_size = cpu_to_dump32(s, sve_size_vq(cpu->sve_max_vq));
22c213
+    note->sve.vl = cpu_to_dump16(s, vq * 16);
22c213
+    note->sve.max_vl = cpu_to_dump16(s, cpu->sve_max_vq * 16);
22c213
+    note->sve.flags = cpu_to_dump16(s, 1);
22c213
+
22c213
+    for (i = 0; i < 32; ++i) {
22c213
+        r = sve_bswap64(tmp, &env->vfp.zregs[i].d[0], vq * 2);
22c213
+        memcpy(&buf[sve_zreg_offset(vq, i)], r, vq * 16);
22c213
+    }
22c213
+
22c213
+    for (i = 0; i < 17; ++i) {
22c213
+        r = sve_bswap64(tmp, r = &env->vfp.pregs[i].p[0],
22c213
+                        DIV_ROUND_UP(vq * 2, 8));
22c213
+        memcpy(&buf[sve_preg_offset(vq, i)], r, vq * 16 / 8);
22c213
+    }
22c213
+
22c213
+    fpr = cpu_to_dump32(s, vfp_get_fpsr(env));
22c213
+    memcpy(&buf[sve_fpsr_offset(vq)], &fpr, sizeof(uint32_t));
22c213
+
22c213
+    fpr = cpu_to_dump32(s, vfp_get_fpcr(env));
22c213
+    memcpy(&buf[sve_fpcr_offset(vq)], &fpr, sizeof(uint32_t));
22c213
+
22c213
+    ret = f(note, AARCH64_SVE_NOTE_SIZE(env), s);
22c213
+    g_free(note);
22c213
+
22c213
+    if (ret < 0) {
22c213
+        return -1;
22c213
+    }
22c213
+
22c213
+    return 0;
22c213
+}
22c213
+#endif
22c213
+
22c213
 int arm_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
22c213
                              int cpuid, void *opaque)
22c213
 {
22c213
     struct aarch64_note note;
22c213
-    CPUARMState *env = &ARM_CPU(cs)->env;
22c213
+    ARMCPU *cpu = ARM_CPU(cs);
22c213
+    CPUARMState *env = &cpu->env;
22c213
     DumpState *s = opaque;
22c213
     uint64_t pstate, sp;
22c213
     int ret, i;
22c213
@@ -163,7 +267,18 @@ int arm_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
22c213
         return -1;
22c213
     }
22c213
 
22c213
-    return aarch64_write_elf64_prfpreg(f, env, cpuid, s);
22c213
+    ret = aarch64_write_elf64_prfpreg(f, env, cpuid, s);
22c213
+    if (ret) {
22c213
+        return ret;
22c213
+    }
22c213
+
22c213
+#ifdef TARGET_AARCH64
22c213
+    if (cpu_isar_feature(aa64_sve, cpu)) {
22c213
+        ret = aarch64_write_elf64_sve(f, env, cpuid, s);
22c213
+    }
22c213
+#endif
22c213
+
22c213
+    return ret;
22c213
 }
22c213
 
22c213
 /* struct pt_regs from arch/arm/include/asm/ptrace.h */
22c213
@@ -335,6 +450,11 @@ ssize_t cpu_get_note_size(int class, int machine, int nr_cpus)
22c213
     if (class == ELFCLASS64) {
22c213
         note_size = AARCH64_PRSTATUS_NOTE_SIZE;
22c213
         note_size += AARCH64_PRFPREG_NOTE_SIZE;
22c213
+#ifdef TARGET_AARCH64
22c213
+        if (cpu_isar_feature(aa64_sve, cpu)) {
22c213
+            note_size += AARCH64_SVE_NOTE_SIZE(env);
22c213
+        }
22c213
+#endif
22c213
     } else {
22c213
         note_size = ARM_PRSTATUS_NOTE_SIZE;
22c213
         if (arm_feature(env, ARM_FEATURE_VFP)) {
22c213
diff --git a/target/arm/cpu.h b/target/arm/cpu.h
22c213
index 83a809d..82dd3cc 100644
22c213
--- a/target/arm/cpu.h
22c213
+++ b/target/arm/cpu.h
22c213
@@ -975,6 +975,31 @@ void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq);
22c213
 void aarch64_sve_change_el(CPUARMState *env, int old_el,
22c213
                            int new_el, bool el0_a64);
22c213
 void aarch64_add_sve_properties(Object *obj);
22c213
+
22c213
+/*
22c213
+ * SVE registers are encoded in KVM's memory in an endianness-invariant format.
22c213
+ * The byte at offset i from the start of the in-memory representation contains
22c213
+ * the bits [(7 + 8 * i) : (8 * i)] of the register value. As this means the
22c213
+ * lowest offsets are stored in the lowest memory addresses, then that nearly
22c213
+ * matches QEMU's representation, which is to use an array of host-endian
22c213
+ * uint64_t's, where the lower offsets are at the lower indices. To complete
22c213
+ * the translation we just need to byte swap the uint64_t's on big-endian hosts.
22c213
+ */
22c213
+static inline uint64_t *sve_bswap64(uint64_t *dst, uint64_t *src, int nr)
22c213
+{
22c213
+#ifdef HOST_WORDS_BIGENDIAN
22c213
+    int i;
22c213
+
22c213
+    for (i = 0; i < nr; ++i) {
22c213
+        dst[i] = bswap64(src[i]);
22c213
+    }
22c213
+
22c213
+    return dst;
22c213
+#else
22c213
+    return src;
22c213
+#endif
22c213
+}
22c213
+
22c213
 #else
22c213
 static inline void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq) { }
22c213
 static inline void aarch64_sve_change_el(CPUARMState *env, int o,
22c213
diff --git a/target/arm/kvm64.c b/target/arm/kvm64.c
22c213
index 876184b..e2da756 100644
22c213
--- a/target/arm/kvm64.c
22c213
+++ b/target/arm/kvm64.c
22c213
@@ -877,30 +877,6 @@ static int kvm_arch_put_fpsimd(CPUState *cs)
22c213
 }
22c213
 
22c213
 /*
22c213
- * SVE registers are encoded in KVM's memory in an endianness-invariant format.
22c213
- * The byte at offset i from the start of the in-memory representation contains
22c213
- * the bits [(7 + 8 * i) : (8 * i)] of the register value. As this means the
22c213
- * lowest offsets are stored in the lowest memory addresses, then that nearly
22c213
- * matches QEMU's representation, which is to use an array of host-endian
22c213
- * uint64_t's, where the lower offsets are at the lower indices. To complete
22c213
- * the translation we just need to byte swap the uint64_t's on big-endian hosts.
22c213
- */
22c213
-static uint64_t *sve_bswap64(uint64_t *dst, uint64_t *src, int nr)
22c213
-{
22c213
-#ifdef HOST_WORDS_BIGENDIAN
22c213
-    int i;
22c213
-
22c213
-    for (i = 0; i < nr; ++i) {
22c213
-        dst[i] = bswap64(src[i]);
22c213
-    }
22c213
-
22c213
-    return dst;
22c213
-#else
22c213
-    return src;
22c213
-#endif
22c213
-}
22c213
-
22c213
-/*
22c213
  * KVM SVE registers come in slices where ZREGs have a slice size of 2048 bits
22c213
  * and PREGS and the FFR have a slice size of 256 bits. However we simply hard
22c213
  * code the slice index to zero for now as it's unlikely we'll need more than
22c213
-- 
22c213
1.8.3.1
22c213