aboutsummaryrefslogtreecommitdiff
path: root/risu_reginfo_aarch64.c
blob: 00d1c8b937eb03e03828067e90ead117d5efbbb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/******************************************************************************
 * Copyright (c) 2013 Linaro Limited
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Claudio Fontana (Linaro) - initial implementation
 *     based on Peter Maydell's risu_arm.c
 *****************************************************************************/

#include <stdio.h>
#include <ucontext.h>
#include <string.h>
#include <signal.h> /* for FPSIMD_MAGIC */
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <inttypes.h>
#include <assert.h>
#include <sys/prctl.h>

#include "risu.h"
#include "risu_reginfo_aarch64.h"

#ifndef SVE_MAGIC
const struct option * const arch_long_opts;
const char * const arch_extra_help;
#else

/* Should we test SVE register state */
static int test_sve;
static const struct option extra_opts[] = {
    {"test-sve", required_argument, NULL, FIRST_ARCH_OPT },
    {0, 0, 0, 0}
};

const struct option * const arch_long_opts = &extra_opts[0];
const char * const arch_extra_help
    = "  --test-sve=<vq>        Compare SVE registers with VQ\n";
#endif

void process_arch_opt(int opt, const char *arg)
{
#ifdef SVE_MAGIC
    long want, got;

    assert(opt == FIRST_ARCH_OPT);
    test_sve = strtol(arg, 0, 10);

    if (test_sve <= 0 || test_sve > SVE_VQ_MAX) {
        fprintf(stderr, "Invalid value for VQ (1-%d)\n", SVE_VQ_MAX);
        exit(1);
    }
    want = sve_vl_from_vq(test_sve);
    got = prctl(PR_SVE_SET_VL, want);
    if (want != got) {
        if (got < 0) {
            perror("prctl PR_SVE_SET_VL");
        } else {
            fprintf(stderr, "Unsupported value for VQ (%d != %d)\n",
                    test_sve, (int)sve_vq_from_vl(got));
        }
        exit(1);
    }
#else
    abort();
#endif
}

const int reginfo_size(void)
{
    int size = offsetof(struct reginfo, simd.end);
#ifdef SVE_MAGIC
    if (test_sve) {
        size = offsetof(struct reginfo, sve.end);
    }
#endif
    return size;
}

/* reginfo_init: initialize with a ucontext */
void reginfo_init(struct reginfo *ri, ucontext_t *uc)
{
    int i;
    struct _aarch64_ctx *ctx, *extra = NULL;
    struct fpsimd_context *fp = NULL;
#ifdef SVE_MAGIC
    struct sve_context *sve = NULL;
#endif

    /* necessary to be able to compare with memcmp later */
    memset(ri, 0, sizeof(*ri));

    for (i = 0; i < 31; i++) {
        ri->regs[i] = uc->uc_mcontext.regs[i];
    }

    ri->sp = 0xdeadbeefdeadbeef;
    ri->pc = uc->uc_mcontext.pc - image_start_address;
    ri->flags = uc->uc_mcontext.pstate & 0xf0000000;    /* get only flags */

    ri->fault_address = uc->uc_mcontext.fault_address;
    ri->faulting_insn = *((uint32_t *) uc->uc_mcontext.pc);

    ctx = (struct _aarch64_ctx *) &uc->uc_mcontext.__reserved[0];
    while (ctx) {
        switch (ctx->magic) {
        case FPSIMD_MAGIC:
            fp = (void *)ctx;
            break;
#ifdef SVE_MAGIC
        case SVE_MAGIC:
            sve = (void *)ctx;
            break;
        case EXTRA_MAGIC:
            extra = (void *)((struct extra_context *)(ctx))->datap;
            break;
#endif
        case 0:
            /* End of list.  */
            ctx = extra;
            extra = NULL;
            continue;
        default:
            /* Unknown record -- skip it.  */
            break;
        }
        ctx = (void *)ctx + ctx->size;
    }

    if (!fp || fp->head.size != sizeof(*fp)) {
        fprintf(stderr, "risu_reginfo_aarch64: failed to get FP/SIMD state\n");
        return;
    }
    ri->fpsr = fp->fpsr;
    ri->fpcr = fp->fpcr;

#ifdef SVE_MAGIC
    if (test_sve) {
        int vq = test_sve;

        if (sve == NULL) {
            fprintf(stderr, "risu_reginfo_aarch64: failed to get SVE state\n");
            return;
        }
        if (sve->vl != sve_vl_from_vq(vq)) {
            fprintf(stderr, "risu_reginfo_aarch64: "
                    "unexpected SVE state: %d != %d\n",
                    sve->vl, sve_vl_from_vq(vq));
            return;
        }

        ri->sve.vl = sve->vl;

        if (sve->head.size < SVE_SIG_CONTEXT_SIZE(vq)) {
            if (sve->head.size == sizeof(*sve)) {
                /* SVE state is empty -- not an error.  */
            } else {
                fprintf(stderr, "risu_reginfo_aarch64: "
                        "failed to get complete SVE state\n");
            }
            return;
        }

        /* Copy ZREG's one at a time */
        for (i = 0; i < SVE_NUM_ZREGS; i++) {
            memcpy(&ri->sve.zregs[i],
                   (void *)sve + SVE_SIG_ZREG_OFFSET(vq, i),
                   SVE_SIG_ZREG_SIZE(vq));
        }

        /* Copy PREG's one at a time */
        for (i = 0; i < SVE_NUM_PREGS; i++) {
            memcpy(&ri->sve.pregs[i],
                   (void *)sve + SVE_SIG_PREG_OFFSET(vq, i),
                   SVE_SIG_PREG_SIZE(vq));
        }

        /* Finally the FFR */
        memcpy(&ri->sve.ffr, (void *)sve + SVE_SIG_FFR_OFFSET(vq),
               SVE_SIG_FFR_SIZE(vq));

        return;
    }
#endif /* SVE_MAGIC */

    for (i = 0; i < 32; i++) {
        ri->simd.vregs[i] = fp->vregs[i];
    }
}

/* reginfo_is_eq: compare the reginfo structs, returns nonzero if equal */
int reginfo_is_eq(struct reginfo *r1, struct reginfo *r2)
{
    return memcmp(r1, r2, reginfo_size()) == 0;
}

#ifdef SVE_MAGIC
static int sve_zreg_is_eq(int vq, const void *z1, const void *z2)
{
    return memcmp(z1, z2, vq * 16) == 0;
}

static int sve_preg_is_eq(int vq, const void *p1, const void *p2)
{
    return memcmp(p1, p2, vq * 2) == 0;
}

static void sve_dump_preg(FILE *f, int vq, const uint16_t *p)
{
    int q;
    for (q = vq - 1; q >= 0; q--) {
        fprintf(f, "%04x", p[q]);
    }
}

static void sve_dump_preg_diff(FILE *f, int vq, const uint16_t *p1,
                               const uint16_t *p2)
{
    sve_dump_preg(f, vq, p1);
    fprintf(f, " vs ");
    sve_dump_preg(f, vq, p2);
    fprintf(f, "\n");
}

static void sve_dump_zreg_diff(FILE *f, int vq, const __uint128_t *z1,
                               const __uint128_t *z2)
{
    const char *pad = "";
    int q;

    for (q = 0; q < vq; ++q) {
        if (z1[q] != z2[q]) {
            fprintf(f, "%sq%-2d: %016" PRIx64 "%016" PRIx64
                    " vs %016" PRIx64 "%016" PRIx64"\n", pad, q,
                    (uint64_t)(z1[q] >> 64), (uint64_t)z1[q],
                    (uint64_t)(z2[q] >> 64), (uint64_t)z2[q]);
            pad = "      ";
        }
    }
}
#endif

/* reginfo_dump: print state to a stream, returns nonzero on success */
int reginfo_dump(struct reginfo *ri, FILE * f)
{
    int i;
    fprintf(f, "  faulting insn %08x\n", ri->faulting_insn);

    for (i = 0; i < 31; i++) {
        fprintf(f, "  X%-2d    : %016" PRIx64 "\n", i, ri->regs[i]);
    }

    fprintf(f, "  sp     : %016" PRIx64 "\n", ri->sp);
    fprintf(f, "  pc     : %016" PRIx64 "\n", ri->pc);
    fprintf(f, "  flags  : %08x\n", ri->flags);
    fprintf(f, "  fpsr   : %08x\n", ri->fpsr);
    fprintf(f, "  fpcr   : %08x\n", ri->fpcr);

#ifdef SVE_MAGIC
    if (test_sve) {
        int q, vq = test_sve;

        fprintf(f, "  vl     : %d\n", ri->sve.vl);

        for (i = 0; i < 32; i++) {
            fprintf(f, "  Z%-2d q%-2d: %016" PRIx64 "%016" PRIx64 "\n", i, 0,
                    (uint64_t)(ri->sve.zregs[i][0] >> 64),
                    (uint64_t)ri->sve.zregs[i][0]);
            for (q = 1; q < vq; ++q) {
                fprintf(f, "      q%-2d: %016" PRIx64 "%016" PRIx64 "\n", q,
                        (uint64_t)(ri->sve.zregs[i][q] >> 64),
                        (uint64_t)ri->sve.zregs[i][q]);
            }
        }

        for (i = 0; i < 16; i++) {
            fprintf(f, "  P%-2d    : ", i);
            sve_dump_preg(f, vq, &ri->sve.pregs[i][0]);
            fprintf(f, "\n");
        }
        fprintf(f, "  FFR    : ");
        sve_dump_preg(f, vq, &ri->sve.ffr[0]);
        fprintf(f, "\n");

        return !ferror(f);
    }
#endif

    for (i = 0; i < 32; i++) {
        fprintf(f, "  V%-2d    : %016" PRIx64 "%016" PRIx64 "\n", i,
                (uint64_t) (ri->simd.vregs[i] >> 64),
                (uint64_t) (ri->simd.vregs[i]));
    }

    return !ferror(f);
}

/* reginfo_dump_mismatch: print mismatch details to a stream, ret nonzero=ok */
int reginfo_dump_mismatch(struct reginfo *m, struct reginfo *a, FILE * f)
{
    int i;
    fprintf(f, "mismatch detail (master : apprentice):\n");
    if (m->faulting_insn != a->faulting_insn) {
        fprintf(f, "  faulting insn mismatch %08x vs %08x\n",
                m->faulting_insn, a->faulting_insn);
    }
    for (i = 0; i < 31; i++) {
        if (m->regs[i] != a->regs[i]) {
            fprintf(f, "  X%-2d    : %016" PRIx64 " vs %016" PRIx64 "\n",
                    i, m->regs[i], a->regs[i]);
        }
    }

    if (m->sp != a->sp) {
        fprintf(f, "  sp     : %016" PRIx64 " vs %016" PRIx64 "\n",
                m->sp, a->sp);
    }

    if (m->pc != a->pc) {
        fprintf(f, "  pc     : %016" PRIx64 " vs %016" PRIx64 "\n",
                m->pc, a->pc);
    }

    if (m->flags != a->flags) {
        fprintf(f, "  flags  : %08x vs %08x\n", m->flags, a->flags);
    }

    if (m->fpsr != a->fpsr) {
        fprintf(f, "  fpsr   : %08x vs %08x\n", m->fpsr, a->fpsr);
    }

    if (m->fpcr != a->fpcr) {
        fprintf(f, "  fpcr   : %08x vs %08x\n", m->fpcr, a->fpcr);
    }

#ifdef SVE_MAGIC
    if (test_sve) {
        int vq = sve_vq_from_vl(m->sve.vl);

        if (m->sve.vl != a->sve.vl) {
            fprintf(f, "  vl    : %d vs %d\n", m->sve.vl, a->sve.vl);
        }

        for (i = 0; i < SVE_NUM_ZREGS; i++) {
            if (!sve_zreg_is_eq(vq, &m->sve.zregs[i], &a->sve.zregs[i])) {
                fprintf(f, "  Z%-2d ", i);
                sve_dump_zreg_diff(f, vq, &m->sve.zregs[i][0],
                                   &a->sve.zregs[i][0]);
            }
        }
        for (i = 0; i < SVE_NUM_PREGS; i++) {
            if (!sve_preg_is_eq(vq, &m->sve.pregs[i], &a->sve.pregs[i])) {
                fprintf(f, "  P%-2d    : ", i);
                sve_dump_preg_diff(f, vq, &m->sve.pregs[i][0],
                                   &a->sve.pregs[i][0]);
            }
        }
        if (!sve_preg_is_eq(vq, &m->sve.ffr, &a->sve.ffr)) {
            fprintf(f, "  FFR   : ");
            sve_dump_preg_diff(f, vq, &m->sve.pregs[i][0], &a->sve.pregs[i][0]);
        }

        return !ferror(f);
    }
#endif

    for (i = 0; i < 32; i++) {
        if (m->simd.vregs[i] != a->simd.vregs[i]) {
            fprintf(f, "  V%-2d    : "
                    "%016" PRIx64 "%016" PRIx64 " vs "
                    "%016" PRIx64 "%016" PRIx64 "\n", i,
                    (uint64_t) (m->simd.vregs[i] >> 64),
                    (uint64_t) m->simd.vregs[i],
                    (uint64_t) (a->simd.vregs[i] >> 64),
                    (uint64_t) a->simd.vregs[i]);
        }
    }

    return !ferror(f);
}