18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2012 ARM Ltd.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
68c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License version 2 as
78c2ecf20Sopenharmony_ci * published by the Free Software Foundation.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful,
108c2ecf20Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
128c2ecf20Sopenharmony_ci * GNU General Public License for more details.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * You should have received a copy of the GNU General Public License
158c2ecf20Sopenharmony_ci * along with this program.  If not, see <http://www.gnu.org/licenses/>.
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_ci#ifndef _UAPI__ASM_SIGCONTEXT_H
188c2ecf20Sopenharmony_ci#define _UAPI__ASM_SIGCONTEXT_H
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include <linux/types.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/*
258c2ecf20Sopenharmony_ci * Signal context structure - contains all info to do with the state
268c2ecf20Sopenharmony_ci * before the signal handler was invoked.
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_cistruct sigcontext {
298c2ecf20Sopenharmony_ci	__u64 fault_address;
308c2ecf20Sopenharmony_ci	/* AArch64 registers */
318c2ecf20Sopenharmony_ci	__u64 regs[31];
328c2ecf20Sopenharmony_ci	__u64 sp;
338c2ecf20Sopenharmony_ci	__u64 pc;
348c2ecf20Sopenharmony_ci	__u64 pstate;
358c2ecf20Sopenharmony_ci	/* 4K reserved for FP/SIMD state and future expansion */
368c2ecf20Sopenharmony_ci	__u8 __reserved[4096] __attribute__((__aligned__(16)));
378c2ecf20Sopenharmony_ci};
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/*
408c2ecf20Sopenharmony_ci * Allocation of __reserved[]:
418c2ecf20Sopenharmony_ci * (Note: records do not necessarily occur in the order shown here.)
428c2ecf20Sopenharmony_ci *
438c2ecf20Sopenharmony_ci *	size		description
448c2ecf20Sopenharmony_ci *
458c2ecf20Sopenharmony_ci *	0x210		fpsimd_context
468c2ecf20Sopenharmony_ci *	 0x10		esr_context
478c2ecf20Sopenharmony_ci *	0x8a0		sve_context (vl <= 64) (optional)
488c2ecf20Sopenharmony_ci *	 0x20		extra_context (optional)
498c2ecf20Sopenharmony_ci *	 0x10		terminator (null _aarch64_ctx)
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci *	0x510		(reserved for future allocation)
528c2ecf20Sopenharmony_ci *
538c2ecf20Sopenharmony_ci * New records that can exceed this space need to be opt-in for userspace, so
548c2ecf20Sopenharmony_ci * that an expanded signal frame is not generated unexpectedly.  The mechanism
558c2ecf20Sopenharmony_ci * for opting in will depend on the extension that generates each new record.
568c2ecf20Sopenharmony_ci * The above table documents the maximum set and sizes of records than can be
578c2ecf20Sopenharmony_ci * generated when userspace does not opt in for any such extension.
588c2ecf20Sopenharmony_ci */
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/*
618c2ecf20Sopenharmony_ci * Header to be used at the beginning of structures extending the user
628c2ecf20Sopenharmony_ci * context. Such structures must be placed after the rt_sigframe on the stack
638c2ecf20Sopenharmony_ci * and be 16-byte aligned. The last structure must be a dummy one with the
648c2ecf20Sopenharmony_ci * magic and size set to 0.
658c2ecf20Sopenharmony_ci */
668c2ecf20Sopenharmony_cistruct _aarch64_ctx {
678c2ecf20Sopenharmony_ci	__u32 magic;
688c2ecf20Sopenharmony_ci	__u32 size;
698c2ecf20Sopenharmony_ci};
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#define FPSIMD_MAGIC	0x46508001
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistruct fpsimd_context {
748c2ecf20Sopenharmony_ci	struct _aarch64_ctx head;
758c2ecf20Sopenharmony_ci	__u32 fpsr;
768c2ecf20Sopenharmony_ci	__u32 fpcr;
778c2ecf20Sopenharmony_ci	__uint128_t vregs[32];
788c2ecf20Sopenharmony_ci};
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/*
818c2ecf20Sopenharmony_ci * Note: similarly to all other integer fields, each V-register is stored in an
828c2ecf20Sopenharmony_ci * endianness-dependent format, with the byte at offset i from the start of the
838c2ecf20Sopenharmony_ci * in-memory representation of the register value containing
848c2ecf20Sopenharmony_ci *
858c2ecf20Sopenharmony_ci *    bits [(7 + 8 * i) : (8 * i)] of the register on little-endian hosts; or
868c2ecf20Sopenharmony_ci *    bits [(127 - 8 * i) : (120 - 8 * i)] on big-endian hosts.
878c2ecf20Sopenharmony_ci */
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/* ESR_EL1 context */
908c2ecf20Sopenharmony_ci#define ESR_MAGIC	0x45535201
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistruct esr_context {
938c2ecf20Sopenharmony_ci	struct _aarch64_ctx head;
948c2ecf20Sopenharmony_ci	__u64 esr;
958c2ecf20Sopenharmony_ci};
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/*
988c2ecf20Sopenharmony_ci * extra_context: describes extra space in the signal frame for
998c2ecf20Sopenharmony_ci * additional structures that don't fit in sigcontext.__reserved[].
1008c2ecf20Sopenharmony_ci *
1018c2ecf20Sopenharmony_ci * Note:
1028c2ecf20Sopenharmony_ci *
1038c2ecf20Sopenharmony_ci * 1) fpsimd_context, esr_context and extra_context must be placed in
1048c2ecf20Sopenharmony_ci * sigcontext.__reserved[] if present.  They cannot be placed in the
1058c2ecf20Sopenharmony_ci * extra space.  Any other record can be placed either in the extra
1068c2ecf20Sopenharmony_ci * space or in sigcontext.__reserved[], unless otherwise specified in
1078c2ecf20Sopenharmony_ci * this file.
1088c2ecf20Sopenharmony_ci *
1098c2ecf20Sopenharmony_ci * 2) There must not be more than one extra_context.
1108c2ecf20Sopenharmony_ci *
1118c2ecf20Sopenharmony_ci * 3) If extra_context is present, it must be followed immediately in
1128c2ecf20Sopenharmony_ci * sigcontext.__reserved[] by the terminating null _aarch64_ctx.
1138c2ecf20Sopenharmony_ci *
1148c2ecf20Sopenharmony_ci * 4) The extra space to which datap points must start at the first
1158c2ecf20Sopenharmony_ci * 16-byte aligned address immediately after the terminating null
1168c2ecf20Sopenharmony_ci * _aarch64_ctx that follows the extra_context structure in
1178c2ecf20Sopenharmony_ci * __reserved[].  The extra space may overrun the end of __reserved[],
1188c2ecf20Sopenharmony_ci * as indicated by a sufficiently large value for the size field.
1198c2ecf20Sopenharmony_ci *
1208c2ecf20Sopenharmony_ci * 5) The extra space must itself be terminated with a null
1218c2ecf20Sopenharmony_ci * _aarch64_ctx.
1228c2ecf20Sopenharmony_ci */
1238c2ecf20Sopenharmony_ci#define EXTRA_MAGIC	0x45585401
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistruct extra_context {
1268c2ecf20Sopenharmony_ci	struct _aarch64_ctx head;
1278c2ecf20Sopenharmony_ci	__u64 datap; /* 16-byte aligned pointer to extra space cast to __u64 */
1288c2ecf20Sopenharmony_ci	__u32 size; /* size in bytes of the extra space */
1298c2ecf20Sopenharmony_ci	__u32 __reserved[3];
1308c2ecf20Sopenharmony_ci};
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci#define SVE_MAGIC	0x53564501
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistruct sve_context {
1358c2ecf20Sopenharmony_ci	struct _aarch64_ctx head;
1368c2ecf20Sopenharmony_ci	__u16 vl;
1378c2ecf20Sopenharmony_ci	__u16 __reserved[3];
1388c2ecf20Sopenharmony_ci};
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci#endif /* !__ASSEMBLY__ */
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci#include <asm/sve_context.h>
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci/*
1458c2ecf20Sopenharmony_ci * The SVE architecture leaves space for future expansion of the
1468c2ecf20Sopenharmony_ci * vector length beyond its initial architectural limit of 2048 bits
1478c2ecf20Sopenharmony_ci * (16 quadwords).
1488c2ecf20Sopenharmony_ci *
1498c2ecf20Sopenharmony_ci * See linux/Documentation/arm64/sve.rst for a description of the VL/VQ
1508c2ecf20Sopenharmony_ci * terminology.
1518c2ecf20Sopenharmony_ci */
1528c2ecf20Sopenharmony_ci#define SVE_VQ_BYTES		__SVE_VQ_BYTES	/* bytes per quadword */
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci#define SVE_VQ_MIN		__SVE_VQ_MIN
1558c2ecf20Sopenharmony_ci#define SVE_VQ_MAX		__SVE_VQ_MAX
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci#define SVE_VL_MIN		__SVE_VL_MIN
1588c2ecf20Sopenharmony_ci#define SVE_VL_MAX		__SVE_VL_MAX
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci#define SVE_NUM_ZREGS		__SVE_NUM_ZREGS
1618c2ecf20Sopenharmony_ci#define SVE_NUM_PREGS		__SVE_NUM_PREGS
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci#define sve_vl_valid(vl)	__sve_vl_valid(vl)
1648c2ecf20Sopenharmony_ci#define sve_vq_from_vl(vl)	__sve_vq_from_vl(vl)
1658c2ecf20Sopenharmony_ci#define sve_vl_from_vq(vq)	__sve_vl_from_vq(vq)
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci/*
1688c2ecf20Sopenharmony_ci * If the SVE registers are currently live for the thread at signal delivery,
1698c2ecf20Sopenharmony_ci * sve_context.head.size >=
1708c2ecf20Sopenharmony_ci *	SVE_SIG_CONTEXT_SIZE(sve_vq_from_vl(sve_context.vl))
1718c2ecf20Sopenharmony_ci * and the register data may be accessed using the SVE_SIG_*() macros.
1728c2ecf20Sopenharmony_ci *
1738c2ecf20Sopenharmony_ci * If sve_context.head.size <
1748c2ecf20Sopenharmony_ci *	SVE_SIG_CONTEXT_SIZE(sve_vq_from_vl(sve_context.vl)),
1758c2ecf20Sopenharmony_ci * the SVE registers were not live for the thread and no register data
1768c2ecf20Sopenharmony_ci * is included: in this case, the SVE_SIG_*() macros should not be
1778c2ecf20Sopenharmony_ci * used except for this check.
1788c2ecf20Sopenharmony_ci *
1798c2ecf20Sopenharmony_ci * The same convention applies when returning from a signal: a caller
1808c2ecf20Sopenharmony_ci * will need to remove or resize the sve_context block if it wants to
1818c2ecf20Sopenharmony_ci * make the SVE registers live when they were previously non-live or
1828c2ecf20Sopenharmony_ci * vice-versa.  This may require the caller to allocate fresh
1838c2ecf20Sopenharmony_ci * memory and/or move other context blocks in the signal frame.
1848c2ecf20Sopenharmony_ci *
1858c2ecf20Sopenharmony_ci * Changing the vector length during signal return is not permitted:
1868c2ecf20Sopenharmony_ci * sve_context.vl must equal the thread's current vector length when
1878c2ecf20Sopenharmony_ci * doing a sigreturn.
1888c2ecf20Sopenharmony_ci *
1898c2ecf20Sopenharmony_ci *
1908c2ecf20Sopenharmony_ci * Note: for all these macros, the "vq" argument denotes the SVE
1918c2ecf20Sopenharmony_ci * vector length in quadwords (i.e., units of 128 bits).
1928c2ecf20Sopenharmony_ci *
1938c2ecf20Sopenharmony_ci * The correct way to obtain vq is to use sve_vq_from_vl(vl).  The
1948c2ecf20Sopenharmony_ci * result is valid if and only if sve_vl_valid(vl) is true.  This is
1958c2ecf20Sopenharmony_ci * guaranteed for a struct sve_context written by the kernel.
1968c2ecf20Sopenharmony_ci *
1978c2ecf20Sopenharmony_ci *
1988c2ecf20Sopenharmony_ci * Additional macros describe the contents and layout of the payload.
1998c2ecf20Sopenharmony_ci * For each, SVE_SIG_x_OFFSET(args) is the start offset relative to
2008c2ecf20Sopenharmony_ci * the start of struct sve_context, and SVE_SIG_x_SIZE(args) is the
2018c2ecf20Sopenharmony_ci * size in bytes:
2028c2ecf20Sopenharmony_ci *
2038c2ecf20Sopenharmony_ci *	x	type				description
2048c2ecf20Sopenharmony_ci *	-	----				-----------
2058c2ecf20Sopenharmony_ci *	REGS					the entire SVE context
2068c2ecf20Sopenharmony_ci *
2078c2ecf20Sopenharmony_ci *	ZREGS	__uint128_t[SVE_NUM_ZREGS][vq]	all Z-registers
2088c2ecf20Sopenharmony_ci *	ZREG	__uint128_t[vq]			individual Z-register Zn
2098c2ecf20Sopenharmony_ci *
2108c2ecf20Sopenharmony_ci *	PREGS	uint16_t[SVE_NUM_PREGS][vq]	all P-registers
2118c2ecf20Sopenharmony_ci *	PREG	uint16_t[vq]			individual P-register Pn
2128c2ecf20Sopenharmony_ci *
2138c2ecf20Sopenharmony_ci *	FFR	uint16_t[vq]			first-fault status register
2148c2ecf20Sopenharmony_ci *
2158c2ecf20Sopenharmony_ci * Additional data might be appended in the future.
2168c2ecf20Sopenharmony_ci *
2178c2ecf20Sopenharmony_ci * Unlike vregs[] in fpsimd_context, each SVE scalable register (Z-, P- or FFR)
2188c2ecf20Sopenharmony_ci * is encoded in memory in an endianness-invariant format, with the byte at
2198c2ecf20Sopenharmony_ci * offset i from the start of the in-memory representation containing bits
2208c2ecf20Sopenharmony_ci * [(7 + 8 * i) : (8 * i)] of the register value.
2218c2ecf20Sopenharmony_ci */
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci#define SVE_SIG_ZREG_SIZE(vq)	__SVE_ZREG_SIZE(vq)
2248c2ecf20Sopenharmony_ci#define SVE_SIG_PREG_SIZE(vq)	__SVE_PREG_SIZE(vq)
2258c2ecf20Sopenharmony_ci#define SVE_SIG_FFR_SIZE(vq)	__SVE_FFR_SIZE(vq)
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci#define SVE_SIG_REGS_OFFSET					\
2288c2ecf20Sopenharmony_ci	((sizeof(struct sve_context) + (__SVE_VQ_BYTES - 1))	\
2298c2ecf20Sopenharmony_ci		/ __SVE_VQ_BYTES * __SVE_VQ_BYTES)
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci#define SVE_SIG_ZREGS_OFFSET \
2328c2ecf20Sopenharmony_ci		(SVE_SIG_REGS_OFFSET + __SVE_ZREGS_OFFSET)
2338c2ecf20Sopenharmony_ci#define SVE_SIG_ZREG_OFFSET(vq, n) \
2348c2ecf20Sopenharmony_ci		(SVE_SIG_REGS_OFFSET + __SVE_ZREG_OFFSET(vq, n))
2358c2ecf20Sopenharmony_ci#define SVE_SIG_ZREGS_SIZE(vq) __SVE_ZREGS_SIZE(vq)
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci#define SVE_SIG_PREGS_OFFSET(vq) \
2388c2ecf20Sopenharmony_ci		(SVE_SIG_REGS_OFFSET + __SVE_PREGS_OFFSET(vq))
2398c2ecf20Sopenharmony_ci#define SVE_SIG_PREG_OFFSET(vq, n) \
2408c2ecf20Sopenharmony_ci		(SVE_SIG_REGS_OFFSET + __SVE_PREG_OFFSET(vq, n))
2418c2ecf20Sopenharmony_ci#define SVE_SIG_PREGS_SIZE(vq) __SVE_PREGS_SIZE(vq)
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci#define SVE_SIG_FFR_OFFSET(vq) \
2448c2ecf20Sopenharmony_ci		(SVE_SIG_REGS_OFFSET + __SVE_FFR_OFFSET(vq))
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci#define SVE_SIG_REGS_SIZE(vq) \
2478c2ecf20Sopenharmony_ci		(__SVE_FFR_OFFSET(vq) + __SVE_FFR_SIZE(vq))
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci#define SVE_SIG_CONTEXT_SIZE(vq) \
2508c2ecf20Sopenharmony_ci		(SVE_SIG_REGS_OFFSET + SVE_SIG_REGS_SIZE(vq))
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci#endif /* _UAPI__ASM_SIGCONTEXT_H */
253