1 //===-- LinuxPTraceDefines_arm64sve.h ------------------------- -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_SOURCE_PLUGINS_PROCESS_UTILITY_LINUXPTRACEDEFINES_ARM64SVE_H
10 #define LLDB_SOURCE_PLUGINS_PROCESS_UTILITY_LINUXPTRACEDEFINES_ARM64SVE_H
11 
12 #include <stdint.h>
13 
14 namespace lldb_private {
15 namespace sve {
16 
17 /*
18  * The SVE architecture leaves space for future expansion of the
19  * vector length beyond its initial architectural limit of 2048 bits
20  * (16 quadwords).
21  *
22  * See <Linux kernel source tree>/Documentation/arm64/sve.rst for a description
23  * of the vl/vq terminology.
24  */
25 
26 const uint16_t vq_bytes = 16; /* number of bytes per quadword */
27 
28 const uint16_t vq_min = 1;
29 const uint16_t vq_max = 512;
30 
31 const uint16_t vl_min = vq_min * vq_bytes;
32 const uint16_t vl_max = vq_max * vq_bytes;
33 
34 const uint16_t num_of_zregs = 32;
35 const uint16_t num_of_pregs = 16;
36 
vl_valid(uint16_t vl)37 inline uint16_t vl_valid(uint16_t vl) {
38   return (vl % vq_bytes == 0 && vl >= vl_min && vl <= vl_max);
39 }
40 
vq_from_vl(uint16_t vl)41 inline uint16_t vq_from_vl(uint16_t vl) { return vl / vq_bytes; }
vl_from_vq(uint16_t vq)42 inline uint16_t vl_from_vq(uint16_t vq) { return vq * vq_bytes; }
43 
44 /* A new signal frame record sve_context encodes the SVE Registers on signal
45  * delivery. sve_context struct definition may be included in asm/sigcontext.h.
46  * We define sve_context_size which will be used by LLDB sve helper functions.
47  * More information on sve_context can be found in Linux kernel source tree at
48  * Documentation/arm64/sve.rst.
49  */
50 
51 const uint16_t sve_context_size = 16;
52 
53 /*
54  * If the SVE registers are currently live for the thread at signal delivery,
55  * sve_context.head.size >=
56  * SigContextSize(vq_from_vl(sve_context.vl))
57  * and the register data may be accessed using the Sig*() functions.
58  *
59  * If sve_context.head.size <
60  * SigContextSize(vq_from_vl(sve_context.vl)),
61  * the SVE registers were not live for the thread and no register data
62  * is included: in this case, the Sig*() functions should not be
63  * used except for this check.
64  *
65  * The same convention applies when returning from a signal: a caller
66  * will need to remove or resize the sve_context block if it wants to
67  * make the SVE registers live when they were previously non-live or
68  * vice-versa.  This may require the the caller to allocate fresh
69  * memory and/or move other context blocks in the signal frame.
70  *
71  * Changing the vector length during signal return is not permitted:
72  * sve_context.vl must equal the thread's current vector length when
73  * doing a sigreturn.
74  *
75  *
76  * Note: for all these functions, the "vq" argument denotes the SVE
77  * vector length in quadwords (i.e., units of 128 bits).
78  *
79  * The correct way to obtain vq is to use vq_from_vl(vl).  The
80  * result is valid if and only if vl_valid(vl) is true.  This is
81  * guaranteed for a struct sve_context written by the kernel.
82  *
83  *
84  * Additional functions describe the contents and layout of the payload.
85  * For each, Sig*Offset(args) is the start offset relative to
86  * the start of struct sve_context, and Sig*Size(args) is the
87  * size in bytes:
88  *
89  *	x	type				description
90  *	-	----				-----------
91  *	REGS					the entire SVE context
92  *
93  *	ZREGS	__uint128_t[num_of_zregs][vq]	all Z-registers
94  *	ZREG	__uint128_t[vq]			individual Z-register Zn
95  *
96  *	PREGS	uint16_t[num_of_pregs][vq]	all P-registers
97  *	PREG	uint16_t[vq]			individual P-register Pn
98  *
99  *	FFR	uint16_t[vq]			first-fault status register
100  *
101  * Additional data might be appended in the future.
102  */
103 
SigZRegSize(uint16_t vq)104 inline uint16_t SigZRegSize(uint16_t vq) { return vq * vq_bytes; }
SigPRegSize(uint16_t vq)105 inline uint16_t SigPRegSize(uint16_t vq) { return vq * vq_bytes / 8; }
SigFFRSize(uint16_t vq)106 inline uint16_t SigFFRSize(uint16_t vq) { return SigPRegSize(vq); }
107 
SigRegsOffset()108 inline uint32_t SigRegsOffset() {
109   return (sve_context_size + vq_bytes - 1) / vq_bytes * vq_bytes;
110 }
111 
SigZRegsOffset()112 inline uint32_t SigZRegsOffset() { return SigRegsOffset(); }
113 
SigZRegOffset(uint16_t vq,uint16_t n)114 inline uint32_t SigZRegOffset(uint16_t vq, uint16_t n) {
115   return SigRegsOffset() + SigZRegSize(vq) * n;
116 }
117 
SigZRegsSize(uint16_t vq)118 inline uint32_t SigZRegsSize(uint16_t vq) {
119   return SigZRegOffset(vq, num_of_zregs) - SigRegsOffset();
120 }
121 
SigPRegsOffset(uint16_t vq)122 inline uint32_t SigPRegsOffset(uint16_t vq) {
123   return SigRegsOffset() + SigZRegsSize(vq);
124 }
125 
SigPRegOffset(uint16_t vq,uint16_t n)126 inline uint32_t SigPRegOffset(uint16_t vq, uint16_t n) {
127   return SigPRegsOffset(vq) + SigPRegSize(vq) * n;
128 }
129 
SigpRegsSize(uint16_t vq)130 inline uint32_t SigpRegsSize(uint16_t vq) {
131   return SigPRegOffset(vq, num_of_pregs) - SigPRegsOffset(vq);
132 }
133 
SigFFROffset(uint16_t vq)134 inline uint32_t SigFFROffset(uint16_t vq) {
135   return SigPRegsOffset(vq) + SigpRegsSize(vq);
136 }
137 
SigRegsSize(uint16_t vq)138 inline uint32_t SigRegsSize(uint16_t vq) {
139   return SigFFROffset(vq) + SigFFRSize(vq) - SigRegsOffset();
140 }
141 
SVESigContextSize(uint16_t vq)142 inline uint32_t SVESigContextSize(uint16_t vq) {
143   return SigRegsOffset() + SigRegsSize(vq);
144 }
145 
146 struct user_sve_header {
147   uint32_t size;     /* total meaningful regset content in bytes */
148   uint32_t max_size; /* maxmium possible size for this thread */
149   uint16_t vl;       /* current vector length */
150   uint16_t max_vl;   /* maximum possible vector length */
151   uint16_t flags;
152   uint16_t reserved;
153 };
154 
155 /* Definitions for user_sve_header.flags: */
156 const uint16_t ptrace_regs_mask = 1 << 0;
157 const uint16_t ptrace_regs_fpsimd = 0;
158 const uint16_t ptrace_regs_sve = ptrace_regs_mask;
159 
160 /*
161  * The remainder of the SVE state follows struct user_sve_header.  The
162  * total size of the SVE state (including header) depends on the
163  * metadata in the header:  PTraceSize(vq, flags) gives the total size
164  * of the state in bytes, including the header.
165  *
166  * Refer to <asm/sigcontext.h> for details of how to pass the correct
167  * "vq" argument to these macros.
168  */
169 
170 /* Offset from the start of struct user_sve_header to the register data */
PTraceRegsOffset()171 inline uint16_t PTraceRegsOffset() {
172   return (sizeof(struct user_sve_header) + vq_bytes - 1) / vq_bytes * vq_bytes;
173 }
174 
175 /*
176  * The register data content and layout depends on the value of the
177  * flags field.
178  */
179 
180 /*
181  * (flags & ptrace_regs_mask) == ptrace_regs_fpsimd case:
182  *
183  * The payload starts at offset PTraceFPSIMDOffset, and is of type
184  * struct user_fpsimd_state.  Additional data might be appended in the
185  * future: use PTraceFPSIMDSize(vq, flags) to compute the total size.
186  * PTraceFPSIMDSize(vq, flags) will never be less than
187  * sizeof(struct user_fpsimd_state).
188  */
189 
190 const uint32_t ptrace_fpsimd_offset = PTraceRegsOffset();
191 
192 /* Return size of struct user_fpsimd_state from asm/ptrace.h */
PTraceFPSIMDSize(uint16_t vq,uint16_t flags)193 inline uint32_t PTraceFPSIMDSize(uint16_t vq, uint16_t flags) { return 528; }
194 
195 /*
196  * (flags & ptrace_regs_mask) == ptrace_regs_sve case:
197  *
198  * The payload starts at offset PTraceSVEOffset, and is of size
199  * PTraceSVESize(vq, flags).
200  *
201  * Additional functions describe the contents and layout of the payload.
202  * For each, PTrace*X*Offset(args) is the start offset relative to
203  * the start of struct user_sve_header, and PTrace*X*Size(args) is
204  * the size in bytes:
205  *
206  *	x	type				description
207  *	-	----				-----------
208  *	ZREGS		\
209  *	ZREG		|
210  *	PREGS		| refer to <asm/sigcontext.h>
211  *	PREG		|
212  *	FFR		/
213  *
214  *	FPSR	uint32_t			FPSR
215  *	FPCR	uint32_t			FPCR
216  *
217  * Additional data might be appended in the future.
218  */
219 
PTraceZRegSize(uint16_t vq)220 inline uint32_t PTraceZRegSize(uint16_t vq) { return SigZRegSize(vq); }
221 
PTracePRegSize(uint16_t vq)222 inline uint32_t PTracePRegSize(uint16_t vq) { return SigPRegSize(vq); }
223 
PTraceFFRSize(uint16_t vq)224 inline uint32_t PTraceFFRSize(uint16_t vq) { return SigFFRSize(vq); }
225 
226 const uint32_t fpsr_size = sizeof(uint32_t);
227 const uint32_t fpcr_size = sizeof(uint32_t);
228 
SigToPTrace(uint32_t offset)229 inline uint32_t SigToPTrace(uint32_t offset) {
230   return offset - SigRegsOffset() + PTraceRegsOffset();
231 }
232 
233 const uint32_t ptrace_sve_offset = PTraceRegsOffset();
234 
PTraceZRegsOffset(uint16_t vq)235 inline uint32_t PTraceZRegsOffset(uint16_t vq) {
236   return SigToPTrace(SigZRegsOffset());
237 }
238 
PTraceZRegOffset(uint16_t vq,uint16_t n)239 inline uint32_t PTraceZRegOffset(uint16_t vq, uint16_t n) {
240   return SigToPTrace(SigZRegOffset(vq, n));
241 }
242 
PTraceZRegsSize(uint16_t vq)243 inline uint32_t PTraceZRegsSize(uint16_t vq) {
244   return PTraceZRegOffset(vq, num_of_zregs) - SigToPTrace(SigRegsOffset());
245 }
246 
PTracePRegsOffset(uint16_t vq)247 inline uint32_t PTracePRegsOffset(uint16_t vq) {
248   return SigToPTrace(SigPRegsOffset(vq));
249 }
250 
PTracePRegOffset(uint16_t vq,uint16_t n)251 inline uint32_t PTracePRegOffset(uint16_t vq, uint16_t n) {
252   return SigToPTrace(SigPRegOffset(vq, n));
253 }
254 
PTracePRegsSize(uint16_t vq)255 inline uint32_t PTracePRegsSize(uint16_t vq) {
256   return PTracePRegOffset(vq, num_of_pregs) - PTracePRegsOffset(vq);
257 }
258 
PTraceFFROffset(uint16_t vq)259 inline uint32_t PTraceFFROffset(uint16_t vq) {
260   return SigToPTrace(SigFFROffset(vq));
261 }
262 
PTraceFPSROffset(uint16_t vq)263 inline uint32_t PTraceFPSROffset(uint16_t vq) {
264   return (PTraceFFROffset(vq) + PTraceFFRSize(vq) + (vq_bytes - 1)) / vq_bytes *
265          vq_bytes;
266 }
267 
PTraceFPCROffset(uint16_t vq)268 inline uint32_t PTraceFPCROffset(uint16_t vq) {
269   return PTraceFPSROffset(vq) + fpsr_size;
270 }
271 
272 /*
273  * Any future extension appended after FPCR must be aligned to the next
274  * 128-bit boundary.
275  */
276 
PTraceSVESize(uint16_t vq,uint16_t flags)277 inline uint32_t PTraceSVESize(uint16_t vq, uint16_t flags) {
278   return (PTraceFPCROffset(vq) + fpcr_size - ptrace_sve_offset + vq_bytes - 1) /
279          vq_bytes * vq_bytes;
280 }
281 
PTraceSize(uint16_t vq,uint16_t flags)282 inline uint32_t PTraceSize(uint16_t vq, uint16_t flags) {
283   return (flags & ptrace_regs_mask) == ptrace_regs_sve
284              ? ptrace_sve_offset + PTraceSVESize(vq, flags)
285              : ptrace_fpsimd_offset + PTraceFPSIMDSize(vq, flags);
286 }
287 
288 } // namespace SVE
289 } // namespace lldb_private
290 
291 #endif // LLDB_SOURCE_PLUGINS_PROCESS_UTILITY_LINUXPTRACEDEFINES_ARM64SVE_H
292