Path: blob/master/tools/testing/selftests/arm64/fp/zt-ptrace.c
29270 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright (C) 2021 ARM Limited.3*/4#include <errno.h>5#include <stdbool.h>6#include <stddef.h>7#include <stdio.h>8#include <stdlib.h>9#include <string.h>10#include <unistd.h>11#include <sys/auxv.h>12#include <sys/prctl.h>13#include <sys/ptrace.h>14#include <sys/types.h>15#include <sys/uio.h>16#include <sys/wait.h>17#include <asm/sigcontext.h>18#include <asm/ptrace.h>1920#include "../../kselftest.h"2122/* <linux/elf.h> and <sys/auxv.h> don't like each other, so: */23#ifndef NT_ARM_ZA24#define NT_ARM_ZA 0x40c25#endif26#ifndef NT_ARM_ZT27#define NT_ARM_ZT 0x40d28#endif2930#define EXPECTED_TESTS 33132static int sme_vl;3334static void fill_buf(char *buf, size_t size)35{36int i;3738for (i = 0; i < size; i++)39buf[i] = random();40}4142static int do_child(void)43{44if (ptrace(PTRACE_TRACEME, -1, NULL, NULL))45ksft_exit_fail_msg("ptrace(PTRACE_TRACEME) failed: %s (%d)\n",46strerror(errno), errno);4748if (raise(SIGSTOP))49ksft_exit_fail_msg("raise(SIGSTOP) failed: %s (%d)\n",50strerror(errno), errno);5152return EXIT_SUCCESS;53}5455static struct user_za_header *get_za(pid_t pid, void **buf, size_t *size)56{57struct user_za_header *za;58void *p;59size_t sz = sizeof(*za);60struct iovec iov;6162while (1) {63if (*size < sz) {64p = realloc(*buf, sz);65if (!p) {66errno = ENOMEM;67goto error;68}6970*buf = p;71*size = sz;72}7374iov.iov_base = *buf;75iov.iov_len = sz;76if (ptrace(PTRACE_GETREGSET, pid, NT_ARM_ZA, &iov))77goto error;7879za = *buf;80if (za->size <= sz)81break;8283sz = za->size;84}8586return za;8788error:89return NULL;90}9192static int set_za(pid_t pid, const struct user_za_header *za)93{94struct iovec iov;9596iov.iov_base = (void *)za;97iov.iov_len = za->size;98return ptrace(PTRACE_SETREGSET, pid, NT_ARM_ZA, &iov);99}100101static int get_zt(pid_t pid, char zt[ZT_SIG_REG_BYTES])102{103struct iovec iov;104105iov.iov_base = zt;106iov.iov_len = ZT_SIG_REG_BYTES;107return ptrace(PTRACE_GETREGSET, pid, NT_ARM_ZT, &iov);108}109110static int set_zt(pid_t pid, const char zt[ZT_SIG_REG_BYTES])111{112struct iovec iov;113114iov.iov_base = (void *)zt;115iov.iov_len = ZT_SIG_REG_BYTES;116return ptrace(PTRACE_SETREGSET, pid, NT_ARM_ZT, &iov);117}118119/* Reading with ZA disabled returns all zeros */120static void ptrace_za_disabled_read_zt(pid_t child)121{122struct user_za_header za;123char zt[ZT_SIG_REG_BYTES];124int ret, i;125bool fail = false;126127/* Disable PSTATE.ZA using the ZA interface */128memset(&za, 0, sizeof(za));129za.vl = sme_vl;130za.size = sizeof(za);131132ret = set_za(child, &za);133if (ret != 0) {134ksft_print_msg("Failed to disable ZA\n");135fail = true;136}137138/* Read back ZT */139ret = get_zt(child, zt);140if (ret != 0) {141ksft_print_msg("Failed to read ZT\n");142fail = true;143}144145for (i = 0; i < ARRAY_SIZE(zt); i++) {146if (zt[i]) {147ksft_print_msg("zt[%d]: 0x%x != 0\n", i, zt[i]);148fail = true;149}150}151152ksft_test_result(!fail, "ptrace_za_disabled_read_zt\n");153}154155/* Writing then reading ZT should return the data written */156static void ptrace_set_get_zt(pid_t child)157{158char zt_in[ZT_SIG_REG_BYTES];159char zt_out[ZT_SIG_REG_BYTES];160int ret, i;161bool fail = false;162163fill_buf(zt_in, sizeof(zt_in));164165ret = set_zt(child, zt_in);166if (ret != 0) {167ksft_print_msg("Failed to set ZT\n");168fail = true;169}170171ret = get_zt(child, zt_out);172if (ret != 0) {173ksft_print_msg("Failed to read ZT\n");174fail = true;175}176177for (i = 0; i < ARRAY_SIZE(zt_in); i++) {178if (zt_in[i] != zt_out[i]) {179ksft_print_msg("zt[%d]: 0x%x != 0x%x\n", i,180zt_in[i], zt_out[i]);181fail = true;182}183}184185ksft_test_result(!fail, "ptrace_set_get_zt\n");186}187188/* Writing ZT should set PSTATE.ZA */189static void ptrace_enable_za_via_zt(pid_t child)190{191struct user_za_header za_in;192struct user_za_header *za_out;193char zt[ZT_SIG_REG_BYTES];194char *za_data;195size_t za_out_size;196int ret, i, vq;197bool fail = false;198199/* Disable PSTATE.ZA using the ZA interface */200memset(&za_in, 0, sizeof(za_in));201za_in.vl = sme_vl;202za_in.size = sizeof(za_in);203204ret = set_za(child, &za_in);205if (ret != 0) {206ksft_print_msg("Failed to disable ZA\n");207fail = true;208}209210/* Write ZT */211fill_buf(zt, sizeof(zt));212ret = set_zt(child, zt);213if (ret != 0) {214ksft_print_msg("Failed to set ZT\n");215fail = true;216}217218/* Read back ZA and check for register data */219za_out = NULL;220za_out_size = 0;221if (get_za(child, (void **)&za_out, &za_out_size)) {222/* Should have an unchanged VL */223if (za_out->vl != sme_vl) {224ksft_print_msg("VL changed from %d to %d\n",225sme_vl, za_out->vl);226fail = true;227}228vq = __sve_vq_from_vl(za_out->vl);229za_data = (char *)za_out + ZA_PT_ZA_OFFSET;230231/* Should have register data */232if (za_out->size < ZA_PT_SIZE(vq)) {233ksft_print_msg("ZA data less than expected: %u < %u\n",234za_out->size, (unsigned int)ZA_PT_SIZE(vq));235fail = true;236vq = 0;237}238239/* That register data should be non-zero */240for (i = 0; i < ZA_PT_ZA_SIZE(vq); i++) {241if (za_data[i]) {242ksft_print_msg("ZA byte %d is %x\n",243i, za_data[i]);244fail = true;245}246}247} else {248ksft_print_msg("Failed to read ZA\n");249fail = true;250}251252ksft_test_result(!fail, "ptrace_enable_za_via_zt\n");253}254255static int do_parent(pid_t child)256{257int ret = EXIT_FAILURE;258pid_t pid;259int status;260siginfo_t si;261262/* Attach to the child */263while (1) {264int sig;265266pid = wait(&status);267if (pid == -1) {268perror("wait");269goto error;270}271272/*273* This should never happen but it's hard to flag in274* the framework.275*/276if (pid != child)277continue;278279if (WIFEXITED(status) || WIFSIGNALED(status))280ksft_exit_fail_msg("Child died unexpectedly\n");281282if (!WIFSTOPPED(status))283goto error;284285sig = WSTOPSIG(status);286287if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &si)) {288if (errno == ESRCH)289goto disappeared;290291if (errno == EINVAL) {292sig = 0; /* bust group-stop */293goto cont;294}295296ksft_test_result_fail("PTRACE_GETSIGINFO: %s\n",297strerror(errno));298goto error;299}300301if (sig == SIGSTOP && si.si_code == SI_TKILL &&302si.si_pid == pid)303break;304305cont:306if (ptrace(PTRACE_CONT, pid, NULL, sig)) {307if (errno == ESRCH)308goto disappeared;309310ksft_test_result_fail("PTRACE_CONT: %s\n",311strerror(errno));312goto error;313}314}315316ksft_print_msg("Parent is %d, child is %d\n", getpid(), child);317318ptrace_za_disabled_read_zt(child);319ptrace_set_get_zt(child);320ptrace_enable_za_via_zt(child);321322ret = EXIT_SUCCESS;323324error:325kill(child, SIGKILL);326327disappeared:328return ret;329}330331int main(void)332{333int ret = EXIT_SUCCESS;334pid_t child;335336srandom(getpid());337338ksft_print_header();339340if (!(getauxval(AT_HWCAP2) & HWCAP2_SME2)) {341ksft_set_plan(1);342ksft_exit_skip("SME2 not available\n");343}344345/* We need a valid SME VL to enable/disable ZA */346sme_vl = prctl(PR_SME_GET_VL);347if (sme_vl == -1) {348ksft_set_plan(1);349ksft_exit_skip("Failed to read SME VL: %d (%s)\n",350errno, strerror(errno));351}352353ksft_set_plan(EXPECTED_TESTS);354355child = fork();356if (!child)357return do_child();358359if (do_parent(child))360ret = EXIT_FAILURE;361362ksft_print_cnts();363364return ret;365}366367368