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