1*8ced9280SMark Brown // SPDX-License-Identifier: GPL-2.0
2*8ced9280SMark Brown /*
3*8ced9280SMark Brown * Copyright (C) 2022 ARM Limited
4*8ced9280SMark Brown *
5*8ced9280SMark Brown * Verify that the TPIDR2 register context in signal frames is set up as
6*8ced9280SMark Brown * expected.
7*8ced9280SMark Brown */
8*8ced9280SMark Brown
9*8ced9280SMark Brown #include <signal.h>
10*8ced9280SMark Brown #include <ucontext.h>
11*8ced9280SMark Brown #include <sys/auxv.h>
12*8ced9280SMark Brown #include <sys/prctl.h>
13*8ced9280SMark Brown #include <unistd.h>
14*8ced9280SMark Brown #include <asm/sigcontext.h>
15*8ced9280SMark Brown
16*8ced9280SMark Brown #include "test_signals_utils.h"
17*8ced9280SMark Brown #include "testcases.h"
18*8ced9280SMark Brown
19*8ced9280SMark Brown static union {
20*8ced9280SMark Brown ucontext_t uc;
21*8ced9280SMark Brown char buf[1024 * 128];
22*8ced9280SMark Brown } context;
23*8ced9280SMark Brown
24*8ced9280SMark Brown #define SYS_TPIDR2 "S3_3_C13_C0_5"
25*8ced9280SMark Brown
get_tpidr2(void)26*8ced9280SMark Brown static uint64_t get_tpidr2(void)
27*8ced9280SMark Brown {
28*8ced9280SMark Brown uint64_t val;
29*8ced9280SMark Brown
30*8ced9280SMark Brown asm volatile (
31*8ced9280SMark Brown "mrs %0, " SYS_TPIDR2 "\n"
32*8ced9280SMark Brown : "=r"(val)
33*8ced9280SMark Brown :
34*8ced9280SMark Brown : "cc");
35*8ced9280SMark Brown
36*8ced9280SMark Brown return val;
37*8ced9280SMark Brown }
38*8ced9280SMark Brown
tpidr2_present(struct tdescr * td,siginfo_t * si,ucontext_t * uc)39*8ced9280SMark Brown int tpidr2_present(struct tdescr *td, siginfo_t *si, ucontext_t *uc)
40*8ced9280SMark Brown {
41*8ced9280SMark Brown struct _aarch64_ctx *head = GET_BUF_RESV_HEAD(context);
42*8ced9280SMark Brown struct tpidr2_context *tpidr2_ctx;
43*8ced9280SMark Brown size_t offset;
44*8ced9280SMark Brown bool in_sigframe;
45*8ced9280SMark Brown bool have_sme;
46*8ced9280SMark Brown __u64 orig_tpidr2;
47*8ced9280SMark Brown
48*8ced9280SMark Brown have_sme = getauxval(AT_HWCAP2) & HWCAP2_SME;
49*8ced9280SMark Brown if (have_sme)
50*8ced9280SMark Brown orig_tpidr2 = get_tpidr2();
51*8ced9280SMark Brown
52*8ced9280SMark Brown if (!get_current_context(td, &context.uc, sizeof(context)))
53*8ced9280SMark Brown return 1;
54*8ced9280SMark Brown
55*8ced9280SMark Brown tpidr2_ctx = (struct tpidr2_context *)
56*8ced9280SMark Brown get_header(head, TPIDR2_MAGIC, td->live_sz, &offset);
57*8ced9280SMark Brown
58*8ced9280SMark Brown in_sigframe = tpidr2_ctx != NULL;
59*8ced9280SMark Brown
60*8ced9280SMark Brown fprintf(stderr, "TPIDR2 sigframe %s on system %s SME\n",
61*8ced9280SMark Brown in_sigframe ? "present" : "absent",
62*8ced9280SMark Brown have_sme ? "with" : "without");
63*8ced9280SMark Brown
64*8ced9280SMark Brown td->pass = (in_sigframe == have_sme);
65*8ced9280SMark Brown
66*8ced9280SMark Brown /*
67*8ced9280SMark Brown * Check that the value we read back was the one present at
68*8ced9280SMark Brown * the time that the signal was triggered. TPIDR2 is owned by
69*8ced9280SMark Brown * libc so we can't safely choose the value and it is possible
70*8ced9280SMark Brown * that we may need to revisit this in future if something
71*8ced9280SMark Brown * starts deciding to set a new TPIDR2 between us reading and
72*8ced9280SMark Brown * the signal.
73*8ced9280SMark Brown */
74*8ced9280SMark Brown if (have_sme && tpidr2_ctx) {
75*8ced9280SMark Brown if (tpidr2_ctx->tpidr2 != orig_tpidr2) {
76*8ced9280SMark Brown fprintf(stderr, "TPIDR2 in frame is %llx, was %llx\n",
77*8ced9280SMark Brown tpidr2_ctx->tpidr2, orig_tpidr2);
78*8ced9280SMark Brown td->pass = false;
79*8ced9280SMark Brown }
80*8ced9280SMark Brown }
81*8ced9280SMark Brown
82*8ced9280SMark Brown return 0;
83*8ced9280SMark Brown }
84*8ced9280SMark Brown
85*8ced9280SMark Brown struct tdescr tde = {
86*8ced9280SMark Brown .name = "TPIDR2",
87*8ced9280SMark Brown .descr = "Validate that TPIDR2 is present as expected",
88*8ced9280SMark Brown .timeout = 3,
89*8ced9280SMark Brown .run = tpidr2_present,
90*8ced9280SMark Brown };
91