sve.rst 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. ===================================================
  2. Scalable Vector Extension support for AArch64 Linux
  3. ===================================================
  4. Author: Dave Martin <[email protected]>
  5. Date: 4 August 2017
  6. This document outlines briefly the interface provided to userspace by Linux in
  7. order to support use of the ARM Scalable Vector Extension (SVE), including
  8. interactions with Streaming SVE mode added by the Scalable Matrix Extension
  9. (SME).
  10. This is an outline of the most important features and issues only and not
  11. intended to be exhaustive.
  12. This document does not aim to describe the SVE architecture or programmer's
  13. model. To aid understanding, a minimal description of relevant programmer's
  14. model features for SVE is included in Appendix A.
  15. 1. General
  16. -----------
  17. * SVE registers Z0..Z31, P0..P15 and FFR and the current vector length VL, are
  18. tracked per-thread.
  19. * In streaming mode FFR is not accessible unless HWCAP2_SME_FA64 is present
  20. in the system, when it is not supported and these interfaces are used to
  21. access streaming mode FFR is read and written as zero.
  22. * The presence of SVE is reported to userspace via HWCAP_SVE in the aux vector
  23. AT_HWCAP entry. Presence of this flag implies the presence of the SVE
  24. instructions and registers, and the Linux-specific system interfaces
  25. described in this document. SVE is reported in /proc/cpuinfo as "sve".
  26. * Support for the execution of SVE instructions in userspace can also be
  27. detected by reading the CPU ID register ID_AA64PFR0_EL1 using an MRS
  28. instruction, and checking that the value of the SVE field is nonzero. [3]
  29. It does not guarantee the presence of the system interfaces described in the
  30. following sections: software that needs to verify that those interfaces are
  31. present must check for HWCAP_SVE instead.
  32. * On hardware that supports the SVE2 extensions, HWCAP2_SVE2 will also
  33. be reported in the AT_HWCAP2 aux vector entry. In addition to this,
  34. optional extensions to SVE2 may be reported by the presence of:
  35. HWCAP2_SVE2
  36. HWCAP2_SVEAES
  37. HWCAP2_SVEPMULL
  38. HWCAP2_SVEBITPERM
  39. HWCAP2_SVESHA3
  40. HWCAP2_SVESM4
  41. This list may be extended over time as the SVE architecture evolves.
  42. These extensions are also reported via the CPU ID register ID_AA64ZFR0_EL1,
  43. which userspace can read using an MRS instruction. See elf_hwcaps.txt and
  44. cpu-feature-registers.txt for details.
  45. * On hardware that supports the SME extensions, HWCAP2_SME will also be
  46. reported in the AT_HWCAP2 aux vector entry. Among other things SME adds
  47. streaming mode which provides a subset of the SVE feature set using a
  48. separate SME vector length and the same Z/V registers. See sme.rst
  49. for more details.
  50. * Debuggers should restrict themselves to interacting with the target via the
  51. NT_ARM_SVE regset. The recommended way of detecting support for this regset
  52. is to connect to a target process first and then attempt a
  53. ptrace(PTRACE_GETREGSET, pid, NT_ARM_SVE, &iov). Note that when SME is
  54. present and streaming SVE mode is in use the FPSIMD subset of registers
  55. will be read via NT_ARM_SVE and NT_ARM_SVE writes will exit streaming mode
  56. in the target.
  57. * Whenever SVE scalable register values (Zn, Pn, FFR) are exchanged in memory
  58. between userspace and the kernel, the register value is encoded in memory in
  59. an endianness-invariant layout, with bits [(8 * i + 7) : (8 * i)] encoded at
  60. byte offset i from the start of the memory representation. This affects for
  61. example the signal frame (struct sve_context) and ptrace interface
  62. (struct user_sve_header) and associated data.
  63. Beware that on big-endian systems this results in a different byte order than
  64. for the FPSIMD V-registers, which are stored as single host-endian 128-bit
  65. values, with bits [(127 - 8 * i) : (120 - 8 * i)] of the register encoded at
  66. byte offset i. (struct fpsimd_context, struct user_fpsimd_state).
  67. 2. Vector length terminology
  68. -----------------------------
  69. The size of an SVE vector (Z) register is referred to as the "vector length".
  70. To avoid confusion about the units used to express vector length, the kernel
  71. adopts the following conventions:
  72. * Vector length (VL) = size of a Z-register in bytes
  73. * Vector quadwords (VQ) = size of a Z-register in units of 128 bits
  74. (So, VL = 16 * VQ.)
  75. The VQ convention is used where the underlying granularity is important, such
  76. as in data structure definitions. In most other situations, the VL convention
  77. is used. This is consistent with the meaning of the "VL" pseudo-register in
  78. the SVE instruction set architecture.
  79. 3. System call behaviour
  80. -------------------------
  81. * On syscall, V0..V31 are preserved (as without SVE). Thus, bits [127:0] of
  82. Z0..Z31 are preserved. All other bits of Z0..Z31, and all of P0..P15 and FFR
  83. become zero on return from a syscall.
  84. * The SVE registers are not used to pass arguments to or receive results from
  85. any syscall.
  86. * In practice the affected registers/bits will be preserved or will be replaced
  87. with zeros on return from a syscall, but userspace should not make
  88. assumptions about this. The kernel behaviour may vary on a case-by-case
  89. basis.
  90. * All other SVE state of a thread, including the currently configured vector
  91. length, the state of the PR_SVE_VL_INHERIT flag, and the deferred vector
  92. length (if any), is preserved across all syscalls, subject to the specific
  93. exceptions for execve() described in section 6.
  94. In particular, on return from a fork() or clone(), the parent and new child
  95. process or thread share identical SVE configuration, matching that of the
  96. parent before the call.
  97. 4. Signal handling
  98. -------------------
  99. * A new signal frame record sve_context encodes the SVE registers on signal
  100. delivery. [1]
  101. * This record is supplementary to fpsimd_context. The FPSR and FPCR registers
  102. are only present in fpsimd_context. For convenience, the content of V0..V31
  103. is duplicated between sve_context and fpsimd_context.
  104. * The record contains a flag field which includes a flag SVE_SIG_FLAG_SM which
  105. if set indicates that the thread is in streaming mode and the vector length
  106. and register data (if present) describe the streaming SVE data and vector
  107. length.
  108. * The signal frame record for SVE always contains basic metadata, in particular
  109. the thread's vector length (in sve_context.vl).
  110. * The SVE registers may or may not be included in the record, depending on
  111. whether the registers are live for the thread. The registers are present if
  112. and only if:
  113. sve_context.head.size >= SVE_SIG_CONTEXT_SIZE(sve_vq_from_vl(sve_context.vl)).
  114. * If the registers are present, the remainder of the record has a vl-dependent
  115. size and layout. Macros SVE_SIG_* are defined [1] to facilitate access to
  116. the members.
  117. * Each scalable register (Zn, Pn, FFR) is stored in an endianness-invariant
  118. layout, with bits [(8 * i + 7) : (8 * i)] stored at byte offset i from the
  119. start of the register's representation in memory.
  120. * If the SVE context is too big to fit in sigcontext.__reserved[], then extra
  121. space is allocated on the stack, an extra_context record is written in
  122. __reserved[] referencing this space. sve_context is then written in the
  123. extra space. Refer to [1] for further details about this mechanism.
  124. 5. Signal return
  125. -----------------
  126. When returning from a signal handler:
  127. * If there is no sve_context record in the signal frame, or if the record is
  128. present but contains no register data as desribed in the previous section,
  129. then the SVE registers/bits become non-live and take unspecified values.
  130. * If sve_context is present in the signal frame and contains full register
  131. data, the SVE registers become live and are populated with the specified
  132. data. However, for backward compatibility reasons, bits [127:0] of Z0..Z31
  133. are always restored from the corresponding members of fpsimd_context.vregs[]
  134. and not from sve_context. The remaining bits are restored from sve_context.
  135. * Inclusion of fpsimd_context in the signal frame remains mandatory,
  136. irrespective of whether sve_context is present or not.
  137. * The vector length cannot be changed via signal return. If sve_context.vl in
  138. the signal frame does not match the current vector length, the signal return
  139. attempt is treated as illegal, resulting in a forced SIGSEGV.
  140. * It is permitted to enter or leave streaming mode by setting or clearing
  141. the SVE_SIG_FLAG_SM flag but applications should take care to ensure that
  142. when doing so sve_context.vl and any register data are appropriate for the
  143. vector length in the new mode.
  144. 6. prctl extensions
  145. --------------------
  146. Some new prctl() calls are added to allow programs to manage the SVE vector
  147. length:
  148. prctl(PR_SVE_SET_VL, unsigned long arg)
  149. Sets the vector length of the calling thread and related flags, where
  150. arg == vl | flags. Other threads of the calling process are unaffected.
  151. vl is the desired vector length, where sve_vl_valid(vl) must be true.
  152. flags:
  153. PR_SVE_VL_INHERIT
  154. Inherit the current vector length across execve(). Otherwise, the
  155. vector length is reset to the system default at execve(). (See
  156. Section 9.)
  157. PR_SVE_SET_VL_ONEXEC
  158. Defer the requested vector length change until the next execve()
  159. performed by this thread.
  160. The effect is equivalent to implicit exceution of the following
  161. call immediately after the next execve() (if any) by the thread:
  162. prctl(PR_SVE_SET_VL, arg & ~PR_SVE_SET_VL_ONEXEC)
  163. This allows launching of a new program with a different vector
  164. length, while avoiding runtime side effects in the caller.
  165. Without PR_SVE_SET_VL_ONEXEC, the requested change takes effect
  166. immediately.
  167. Return value: a nonnegative on success, or a negative value on error:
  168. EINVAL: SVE not supported, invalid vector length requested, or
  169. invalid flags.
  170. On success:
  171. * Either the calling thread's vector length or the deferred vector length
  172. to be applied at the next execve() by the thread (dependent on whether
  173. PR_SVE_SET_VL_ONEXEC is present in arg), is set to the largest value
  174. supported by the system that is less than or equal to vl. If vl ==
  175. SVE_VL_MAX, the value set will be the largest value supported by the
  176. system.
  177. * Any previously outstanding deferred vector length change in the calling
  178. thread is cancelled.
  179. * The returned value describes the resulting configuration, encoded as for
  180. PR_SVE_GET_VL. The vector length reported in this value is the new
  181. current vector length for this thread if PR_SVE_SET_VL_ONEXEC was not
  182. present in arg; otherwise, the reported vector length is the deferred
  183. vector length that will be applied at the next execve() by the calling
  184. thread.
  185. * Changing the vector length causes all of P0..P15, FFR and all bits of
  186. Z0..Z31 except for Z0 bits [127:0] .. Z31 bits [127:0] to become
  187. unspecified. Calling PR_SVE_SET_VL with vl equal to the thread's current
  188. vector length, or calling PR_SVE_SET_VL with the PR_SVE_SET_VL_ONEXEC
  189. flag, does not constitute a change to the vector length for this purpose.
  190. prctl(PR_SVE_GET_VL)
  191. Gets the vector length of the calling thread.
  192. The following flag may be OR-ed into the result:
  193. PR_SVE_VL_INHERIT
  194. Vector length will be inherited across execve().
  195. There is no way to determine whether there is an outstanding deferred
  196. vector length change (which would only normally be the case between a
  197. fork() or vfork() and the corresponding execve() in typical use).
  198. To extract the vector length from the result, bitwise and it with
  199. PR_SVE_VL_LEN_MASK.
  200. Return value: a nonnegative value on success, or a negative value on error:
  201. EINVAL: SVE not supported.
  202. 7. ptrace extensions
  203. ---------------------
  204. * New regsets NT_ARM_SVE and NT_ARM_SSVE are defined for use with
  205. PTRACE_GETREGSET and PTRACE_SETREGSET. NT_ARM_SSVE describes the
  206. streaming mode SVE registers and NT_ARM_SVE describes the
  207. non-streaming mode SVE registers.
  208. In this description a register set is referred to as being "live" when
  209. the target is in the appropriate streaming or non-streaming mode and is
  210. using data beyond the subset shared with the FPSIMD Vn registers.
  211. Refer to [2] for definitions.
  212. The regset data starts with struct user_sve_header, containing:
  213. size
  214. Size of the complete regset, in bytes.
  215. This depends on vl and possibly on other things in the future.
  216. If a call to PTRACE_GETREGSET requests less data than the value of
  217. size, the caller can allocate a larger buffer and retry in order to
  218. read the complete regset.
  219. max_size
  220. Maximum size in bytes that the regset can grow to for the target
  221. thread. The regset won't grow bigger than this even if the target
  222. thread changes its vector length etc.
  223. vl
  224. Target thread's current vector length, in bytes.
  225. max_vl
  226. Maximum possible vector length for the target thread.
  227. flags
  228. at most one of
  229. SVE_PT_REGS_FPSIMD
  230. SVE registers are not live (GETREGSET) or are to be made
  231. non-live (SETREGSET).
  232. The payload is of type struct user_fpsimd_state, with the same
  233. meaning as for NT_PRFPREG, starting at offset
  234. SVE_PT_FPSIMD_OFFSET from the start of user_sve_header.
  235. Extra data might be appended in the future: the size of the
  236. payload should be obtained using SVE_PT_FPSIMD_SIZE(vq, flags).
  237. vq should be obtained using sve_vq_from_vl(vl).
  238. or
  239. SVE_PT_REGS_SVE
  240. SVE registers are live (GETREGSET) or are to be made live
  241. (SETREGSET).
  242. The payload contains the SVE register data, starting at offset
  243. SVE_PT_SVE_OFFSET from the start of user_sve_header, and with
  244. size SVE_PT_SVE_SIZE(vq, flags);
  245. ... OR-ed with zero or more of the following flags, which have the same
  246. meaning and behaviour as the corresponding PR_SET_VL_* flags:
  247. SVE_PT_VL_INHERIT
  248. SVE_PT_VL_ONEXEC (SETREGSET only).
  249. If neither FPSIMD nor SVE flags are provided then no register
  250. payload is available, this is only possible when SME is implemented.
  251. * The effects of changing the vector length and/or flags are equivalent to
  252. those documented for PR_SVE_SET_VL.
  253. The caller must make a further GETREGSET call if it needs to know what VL is
  254. actually set by SETREGSET, unless is it known in advance that the requested
  255. VL is supported.
  256. * In the SVE_PT_REGS_SVE case, the size and layout of the payload depends on
  257. the header fields. The SVE_PT_SVE_*() macros are provided to facilitate
  258. access to the members.
  259. * In either case, for SETREGSET it is permissible to omit the payload, in which
  260. case only the vector length and flags are changed (along with any
  261. consequences of those changes).
  262. * In systems supporting SME when in streaming mode a GETREGSET for
  263. NT_REG_SVE will return only the user_sve_header with no register data,
  264. similarly a GETREGSET for NT_REG_SSVE will not return any register data
  265. when not in streaming mode.
  266. * A GETREGSET for NT_ARM_SSVE will never return SVE_PT_REGS_FPSIMD.
  267. * For SETREGSET, if an SVE_PT_REGS_SVE payload is present and the
  268. requested VL is not supported, the effect will be the same as if the
  269. payload were omitted, except that an EIO error is reported. No
  270. attempt is made to translate the payload data to the correct layout
  271. for the vector length actually set. The thread's FPSIMD state is
  272. preserved, but the remaining bits of the SVE registers become
  273. unspecified. It is up to the caller to translate the payload layout
  274. for the actual VL and retry.
  275. * Where SME is implemented it is not possible to GETREGSET the register
  276. state for normal SVE when in streaming mode, nor the streaming mode
  277. register state when in normal mode, regardless of the implementation defined
  278. behaviour of the hardware for sharing data between the two modes.
  279. * Any SETREGSET of NT_ARM_SVE will exit streaming mode if the target was in
  280. streaming mode and any SETREGSET of NT_ARM_SSVE will enter streaming mode
  281. if the target was not in streaming mode.
  282. * The effect of writing a partial, incomplete payload is unspecified.
  283. 8. ELF coredump extensions
  284. ---------------------------
  285. * NT_ARM_SVE and NT_ARM_SSVE notes will be added to each coredump for
  286. each thread of the dumped process. The contents will be equivalent to the
  287. data that would have been read if a PTRACE_GETREGSET of the corresponding
  288. type were executed for each thread when the coredump was generated.
  289. 9. System runtime configuration
  290. --------------------------------
  291. * To mitigate the ABI impact of expansion of the signal frame, a policy
  292. mechanism is provided for administrators, distro maintainers and developers
  293. to set the default vector length for userspace processes:
  294. /proc/sys/abi/sve_default_vector_length
  295. Writing the text representation of an integer to this file sets the system
  296. default vector length to the specified value, unless the value is greater
  297. than the maximum vector length supported by the system in which case the
  298. default vector length is set to that maximum.
  299. The result can be determined by reopening the file and reading its
  300. contents.
  301. At boot, the default vector length is initially set to 64 or the maximum
  302. supported vector length, whichever is smaller. This determines the initial
  303. vector length of the init process (PID 1).
  304. Reading this file returns the current system default vector length.
  305. * At every execve() call, the new vector length of the new process is set to
  306. the system default vector length, unless
  307. * PR_SVE_VL_INHERIT (or equivalently SVE_PT_VL_INHERIT) is set for the
  308. calling thread, or
  309. * a deferred vector length change is pending, established via the
  310. PR_SVE_SET_VL_ONEXEC flag (or SVE_PT_VL_ONEXEC).
  311. * Modifying the system default vector length does not affect the vector length
  312. of any existing process or thread that does not make an execve() call.
  313. 10. Perf extensions
  314. --------------------------------
  315. * The arm64 specific DWARF standard [5] added the VG (Vector Granule) register
  316. at index 46. This register is used for DWARF unwinding when variable length
  317. SVE registers are pushed onto the stack.
  318. * Its value is equivalent to the current SVE vector length (VL) in bits divided
  319. by 64.
  320. * The value is included in Perf samples in the regs[46] field if
  321. PERF_SAMPLE_REGS_USER is set and the sample_regs_user mask has bit 46 set.
  322. * The value is the current value at the time the sample was taken, and it can
  323. change over time.
  324. * If the system doesn't support SVE when perf_event_open is called with these
  325. settings, the event will fail to open.
  326. Appendix A. SVE programmer's model (informative)
  327. =================================================
  328. This section provides a minimal description of the additions made by SVE to the
  329. ARMv8-A programmer's model that are relevant to this document.
  330. Note: This section is for information only and not intended to be complete or
  331. to replace any architectural specification.
  332. A.1. Registers
  333. ---------------
  334. In A64 state, SVE adds the following:
  335. * 32 8VL-bit vector registers Z0..Z31
  336. For each Zn, Zn bits [127:0] alias the ARMv8-A vector register Vn.
  337. A register write using a Vn register name zeros all bits of the corresponding
  338. Zn except for bits [127:0].
  339. * 16 VL-bit predicate registers P0..P15
  340. * 1 VL-bit special-purpose predicate register FFR (the "first-fault register")
  341. * a VL "pseudo-register" that determines the size of each vector register
  342. The SVE instruction set architecture provides no way to write VL directly.
  343. Instead, it can be modified only by EL1 and above, by writing appropriate
  344. system registers.
  345. * The value of VL can be configured at runtime by EL1 and above:
  346. 16 <= VL <= VLmax, where VL must be a multiple of 16.
  347. * The maximum vector length is determined by the hardware:
  348. 16 <= VLmax <= 256.
  349. (The SVE architecture specifies 256, but permits future architecture
  350. revisions to raise this limit.)
  351. * FPSR and FPCR are retained from ARMv8-A, and interact with SVE floating-point
  352. operations in a similar way to the way in which they interact with ARMv8
  353. floating-point operations::
  354. 8VL-1 128 0 bit index
  355. +---- //// -----------------+
  356. Z0 | : V0 |
  357. : :
  358. Z7 | : V7 |
  359. Z8 | : * V8 |
  360. : : :
  361. Z15 | : *V15 |
  362. Z16 | : V16 |
  363. : :
  364. Z31 | : V31 |
  365. +---- //// -----------------+
  366. 31 0
  367. VL-1 0 +-------+
  368. +---- //// --+ FPSR | |
  369. P0 | | +-------+
  370. : | | *FPCR | |
  371. P15 | | +-------+
  372. +---- //// --+
  373. FFR | | +-----+
  374. +---- //// --+ VL | |
  375. +-----+
  376. (*) callee-save:
  377. This only applies to bits [63:0] of Z-/V-registers.
  378. FPCR contains callee-save and caller-save bits. See [4] for details.
  379. A.2. Procedure call standard
  380. -----------------------------
  381. The ARMv8-A base procedure call standard is extended as follows with respect to
  382. the additional SVE register state:
  383. * All SVE register bits that are not shared with FP/SIMD are caller-save.
  384. * Z8 bits [63:0] .. Z15 bits [63:0] are callee-save.
  385. This follows from the way these bits are mapped to V8..V15, which are caller-
  386. save in the base procedure call standard.
  387. Appendix B. ARMv8-A FP/SIMD programmer's model
  388. ===============================================
  389. Note: This section is for information only and not intended to be complete or
  390. to replace any architectural specification.
  391. Refer to [4] for more information.
  392. ARMv8-A defines the following floating-point / SIMD register state:
  393. * 32 128-bit vector registers V0..V31
  394. * 2 32-bit status/control registers FPSR, FPCR
  395. ::
  396. 127 0 bit index
  397. +---------------+
  398. V0 | |
  399. : : :
  400. V7 | |
  401. * V8 | |
  402. : : : :
  403. *V15 | |
  404. V16 | |
  405. : : :
  406. V31 | |
  407. +---------------+
  408. 31 0
  409. +-------+
  410. FPSR | |
  411. +-------+
  412. *FPCR | |
  413. +-------+
  414. (*) callee-save:
  415. This only applies to bits [63:0] of V-registers.
  416. FPCR contains a mixture of callee-save and caller-save bits.
  417. References
  418. ==========
  419. [1] arch/arm64/include/uapi/asm/sigcontext.h
  420. AArch64 Linux signal ABI definitions
  421. [2] arch/arm64/include/uapi/asm/ptrace.h
  422. AArch64 Linux ptrace ABI definitions
  423. [3] Documentation/arm64/cpu-feature-registers.rst
  424. [4] ARM IHI0055C
  425. http://infocenter.arm.com/help/topic/com.arm.doc.ihi0055c/IHI0055C_beta_aapcs64.pdf
  426. http://infocenter.arm.com/help/topic/com.arm.doc.subset.swdev.abi/index.html
  427. Procedure Call Standard for the ARM 64-bit Architecture (AArch64)
  428. [5] https://github.com/ARM-software/abi-aa/blob/main/aadwarf64/aadwarf64.rst