sme.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. ===================================================
  2. Scalable Matrix Extension support for AArch64 Linux
  3. ===================================================
  4. This document outlines briefly the interface provided to userspace by Linux in
  5. order to support use of the ARM Scalable Matrix Extension (SME).
  6. This is an outline of the most important features and issues only and not
  7. intended to be exhaustive. It should be read in conjunction with the SVE
  8. documentation in sve.rst which provides details on the Streaming SVE mode
  9. included in SME.
  10. This document does not aim to describe the SME architecture or programmer's
  11. model. To aid understanding, a minimal description of relevant programmer's
  12. model features for SME is included in Appendix A.
  13. 1. General
  14. -----------
  15. * PSTATE.SM, PSTATE.ZA, the streaming mode vector length, the ZA
  16. register state and TPIDR2_EL0 are tracked per thread.
  17. * The presence of SME is reported to userspace via HWCAP2_SME in the aux vector
  18. AT_HWCAP2 entry. Presence of this flag implies the presence of the SME
  19. instructions and registers, and the Linux-specific system interfaces
  20. described in this document. SME is reported in /proc/cpuinfo as "sme".
  21. * Support for the execution of SME instructions in userspace can also be
  22. detected by reading the CPU ID register ID_AA64PFR1_EL1 using an MRS
  23. instruction, and checking that the value of the SME field is nonzero. [3]
  24. It does not guarantee the presence of the system interfaces described in the
  25. following sections: software that needs to verify that those interfaces are
  26. present must check for HWCAP2_SME instead.
  27. * There are a number of optional SME features, presence of these is reported
  28. through AT_HWCAP2 through:
  29. HWCAP2_SME_I16I64
  30. HWCAP2_SME_F64F64
  31. HWCAP2_SME_I8I32
  32. HWCAP2_SME_F16F32
  33. HWCAP2_SME_B16F32
  34. HWCAP2_SME_F32F32
  35. HWCAP2_SME_FA64
  36. This list may be extended over time as the SME architecture evolves.
  37. These extensions are also reported via the CPU ID register ID_AA64SMFR0_EL1,
  38. which userspace can read using an MRS instruction. See elf_hwcaps.txt and
  39. cpu-feature-registers.txt for details.
  40. * Debuggers should restrict themselves to interacting with the target via the
  41. NT_ARM_SVE, NT_ARM_SSVE and NT_ARM_ZA regsets. The recommended way
  42. of detecting support for these regsets is to connect to a target process
  43. first and then attempt a
  44. ptrace(PTRACE_GETREGSET, pid, NT_ARM_<regset>, &iov).
  45. * Whenever ZA register values are exchanged in memory between userspace and
  46. the kernel, the register value is encoded in memory as a series of horizontal
  47. vectors from 0 to VL/8-1 stored in the same endianness invariant format as is
  48. used for SVE vectors.
  49. * On thread creation TPIDR2_EL0 is preserved unless CLONE_SETTLS is specified,
  50. in which case it is set to 0.
  51. 2. Vector lengths
  52. ------------------
  53. SME defines a second vector length similar to the SVE vector length which is
  54. controls the size of the streaming mode SVE vectors and the ZA matrix array.
  55. The ZA matrix is square with each side having as many bytes as a streaming
  56. mode SVE vector.
  57. 3. Sharing of streaming and non-streaming mode SVE state
  58. ---------------------------------------------------------
  59. It is implementation defined which if any parts of the SVE state are shared
  60. between streaming and non-streaming modes. When switching between modes
  61. via software interfaces such as ptrace if no register content is provided as
  62. part of switching no state will be assumed to be shared and everything will
  63. be zeroed.
  64. 4. System call behaviour
  65. -------------------------
  66. * On syscall PSTATE.ZA is preserved, if PSTATE.ZA==1 then the contents of the
  67. ZA matrix are preserved.
  68. * On syscall PSTATE.SM will be cleared and the SVE registers will be handled
  69. as per the standard SVE ABI.
  70. * Neither the SVE registers nor ZA are used to pass arguments to or receive
  71. results from any syscall.
  72. * On process creation (eg, clone()) the newly created process will have
  73. PSTATE.SM cleared.
  74. * All other SME state of a thread, including the currently configured vector
  75. length, the state of the PR_SME_VL_INHERIT flag, and the deferred vector
  76. length (if any), is preserved across all syscalls, subject to the specific
  77. exceptions for execve() described in section 6.
  78. 5. Signal handling
  79. -------------------
  80. * Signal handlers are invoked with streaming mode and ZA disabled.
  81. * A new signal frame record za_context encodes the ZA register contents on
  82. signal delivery. [1]
  83. * The signal frame record for ZA always contains basic metadata, in particular
  84. the thread's vector length (in za_context.vl).
  85. * The ZA matrix may or may not be included in the record, depending on
  86. the value of PSTATE.ZA. The registers are present if and only if:
  87. za_context.head.size >= ZA_SIG_CONTEXT_SIZE(sve_vq_from_vl(za_context.vl))
  88. in which case PSTATE.ZA == 1.
  89. * If matrix data is present, the remainder of the record has a vl-dependent
  90. size and layout. Macros ZA_SIG_* are defined [1] to facilitate access to
  91. them.
  92. * The matrix is stored as a series of horizontal vectors in the same format as
  93. is used for SVE vectors.
  94. * If the ZA context is too big to fit in sigcontext.__reserved[], then extra
  95. space is allocated on the stack, an extra_context record is written in
  96. __reserved[] referencing this space. za_context is then written in the
  97. extra space. Refer to [1] for further details about this mechanism.
  98. 5. Signal return
  99. -----------------
  100. When returning from a signal handler:
  101. * If there is no za_context record in the signal frame, or if the record is
  102. present but contains no register data as described in the previous section,
  103. then ZA is disabled.
  104. * If za_context is present in the signal frame and contains matrix data then
  105. PSTATE.ZA is set to 1 and ZA is populated with the specified data.
  106. * The vector length cannot be changed via signal return. If za_context.vl in
  107. the signal frame does not match the current vector length, the signal return
  108. attempt is treated as illegal, resulting in a forced SIGSEGV.
  109. 6. prctl extensions
  110. --------------------
  111. Some new prctl() calls are added to allow programs to manage the SME vector
  112. length:
  113. prctl(PR_SME_SET_VL, unsigned long arg)
  114. Sets the vector length of the calling thread and related flags, where
  115. arg == vl | flags. Other threads of the calling process are unaffected.
  116. vl is the desired vector length, where sve_vl_valid(vl) must be true.
  117. flags:
  118. PR_SME_VL_INHERIT
  119. Inherit the current vector length across execve(). Otherwise, the
  120. vector length is reset to the system default at execve(). (See
  121. Section 9.)
  122. PR_SME_SET_VL_ONEXEC
  123. Defer the requested vector length change until the next execve()
  124. performed by this thread.
  125. The effect is equivalent to implicit execution of the following
  126. call immediately after the next execve() (if any) by the thread:
  127. prctl(PR_SME_SET_VL, arg & ~PR_SME_SET_VL_ONEXEC)
  128. This allows launching of a new program with a different vector
  129. length, while avoiding runtime side effects in the caller.
  130. Without PR_SME_SET_VL_ONEXEC, the requested change takes effect
  131. immediately.
  132. Return value: a nonnegative on success, or a negative value on error:
  133. EINVAL: SME not supported, invalid vector length requested, or
  134. invalid flags.
  135. On success:
  136. * Either the calling thread's vector length or the deferred vector length
  137. to be applied at the next execve() by the thread (dependent on whether
  138. PR_SME_SET_VL_ONEXEC is present in arg), is set to the largest value
  139. supported by the system that is less than or equal to vl. If vl ==
  140. SVE_VL_MAX, the value set will be the largest value supported by the
  141. system.
  142. * Any previously outstanding deferred vector length change in the calling
  143. thread is cancelled.
  144. * The returned value describes the resulting configuration, encoded as for
  145. PR_SME_GET_VL. The vector length reported in this value is the new
  146. current vector length for this thread if PR_SME_SET_VL_ONEXEC was not
  147. present in arg; otherwise, the reported vector length is the deferred
  148. vector length that will be applied at the next execve() by the calling
  149. thread.
  150. * Changing the vector length causes all of ZA, P0..P15, FFR and all bits of
  151. Z0..Z31 except for Z0 bits [127:0] .. Z31 bits [127:0] to become
  152. unspecified, including both streaming and non-streaming SVE state.
  153. Calling PR_SME_SET_VL with vl equal to the thread's current vector
  154. length, or calling PR_SME_SET_VL with the PR_SVE_SET_VL_ONEXEC flag,
  155. does not constitute a change to the vector length for this purpose.
  156. * Changing the vector length causes PSTATE.ZA and PSTATE.SM to be cleared.
  157. Calling PR_SME_SET_VL with vl equal to the thread's current vector
  158. length, or calling PR_SME_SET_VL with the PR_SVE_SET_VL_ONEXEC flag,
  159. does not constitute a change to the vector length for this purpose.
  160. prctl(PR_SME_GET_VL)
  161. Gets the vector length of the calling thread.
  162. The following flag may be OR-ed into the result:
  163. PR_SME_VL_INHERIT
  164. Vector length will be inherited across execve().
  165. There is no way to determine whether there is an outstanding deferred
  166. vector length change (which would only normally be the case between a
  167. fork() or vfork() and the corresponding execve() in typical use).
  168. To extract the vector length from the result, bitwise and it with
  169. PR_SME_VL_LEN_MASK.
  170. Return value: a nonnegative value on success, or a negative value on error:
  171. EINVAL: SME not supported.
  172. 7. ptrace extensions
  173. ---------------------
  174. * A new regset NT_ARM_SSVE is defined for access to streaming mode SVE
  175. state via PTRACE_GETREGSET and PTRACE_SETREGSET, this is documented in
  176. sve.rst.
  177. * A new regset NT_ARM_ZA is defined for ZA state for access to ZA state via
  178. PTRACE_GETREGSET and PTRACE_SETREGSET.
  179. Refer to [2] for definitions.
  180. The regset data starts with struct user_za_header, containing:
  181. size
  182. Size of the complete regset, in bytes.
  183. This depends on vl and possibly on other things in the future.
  184. If a call to PTRACE_GETREGSET requests less data than the value of
  185. size, the caller can allocate a larger buffer and retry in order to
  186. read the complete regset.
  187. max_size
  188. Maximum size in bytes that the regset can grow to for the target
  189. thread. The regset won't grow bigger than this even if the target
  190. thread changes its vector length etc.
  191. vl
  192. Target thread's current streaming vector length, in bytes.
  193. max_vl
  194. Maximum possible streaming vector length for the target thread.
  195. flags
  196. Zero or more of the following flags, which have the same
  197. meaning and behaviour as the corresponding PR_SET_VL_* flags:
  198. SME_PT_VL_INHERIT
  199. SME_PT_VL_ONEXEC (SETREGSET only).
  200. * The effects of changing the vector length and/or flags are equivalent to
  201. those documented for PR_SME_SET_VL.
  202. The caller must make a further GETREGSET call if it needs to know what VL is
  203. actually set by SETREGSET, unless is it known in advance that the requested
  204. VL is supported.
  205. * The size and layout of the payload depends on the header fields. The
  206. SME_PT_ZA_*() macros are provided to facilitate access to the data.
  207. * In either case, for SETREGSET it is permissible to omit the payload, in which
  208. case the vector length and flags are changed and PSTATE.ZA is set to 0
  209. (along with any consequences of those changes). If a payload is provided
  210. then PSTATE.ZA will be set to 1.
  211. * For SETREGSET, if the requested VL is not supported, the effect will be the
  212. same as if the payload were omitted, except that an EIO error is reported.
  213. No attempt is made to translate the payload data to the correct layout
  214. for the vector length actually set. It is up to the caller to translate the
  215. payload layout for the actual VL and retry.
  216. * The effect of writing a partial, incomplete payload is unspecified.
  217. 8. ELF coredump extensions
  218. ---------------------------
  219. * NT_ARM_SSVE notes will be added to each coredump for
  220. each thread of the dumped process. The contents will be equivalent to the
  221. data that would have been read if a PTRACE_GETREGSET of the corresponding
  222. type were executed for each thread when the coredump was generated.
  223. * A NT_ARM_ZA note will be added to each coredump for each thread of the
  224. dumped process. The contents will be equivalent to the data that would have
  225. been read if a PTRACE_GETREGSET of NT_ARM_ZA were executed for each thread
  226. when the coredump was generated.
  227. * The NT_ARM_TLS note will be extended to two registers, the second register
  228. will contain TPIDR2_EL0 on systems that support SME and will be read as
  229. zero with writes ignored otherwise.
  230. 9. System runtime configuration
  231. --------------------------------
  232. * To mitigate the ABI impact of expansion of the signal frame, a policy
  233. mechanism is provided for administrators, distro maintainers and developers
  234. to set the default vector length for userspace processes:
  235. /proc/sys/abi/sme_default_vector_length
  236. Writing the text representation of an integer to this file sets the system
  237. default vector length to the specified value, unless the value is greater
  238. than the maximum vector length supported by the system in which case the
  239. default vector length is set to that maximum.
  240. The result can be determined by reopening the file and reading its
  241. contents.
  242. At boot, the default vector length is initially set to 32 or the maximum
  243. supported vector length, whichever is smaller and supported. This
  244. determines the initial vector length of the init process (PID 1).
  245. Reading this file returns the current system default vector length.
  246. * At every execve() call, the new vector length of the new process is set to
  247. the system default vector length, unless
  248. * PR_SME_VL_INHERIT (or equivalently SME_PT_VL_INHERIT) is set for the
  249. calling thread, or
  250. * a deferred vector length change is pending, established via the
  251. PR_SME_SET_VL_ONEXEC flag (or SME_PT_VL_ONEXEC).
  252. * Modifying the system default vector length does not affect the vector length
  253. of any existing process or thread that does not make an execve() call.
  254. Appendix A. SME programmer's model (informative)
  255. =================================================
  256. This section provides a minimal description of the additions made by SME to the
  257. ARMv8-A programmer's model that are relevant to this document.
  258. Note: This section is for information only and not intended to be complete or
  259. to replace any architectural specification.
  260. A.1. Registers
  261. ---------------
  262. In A64 state, SME adds the following:
  263. * A new mode, streaming mode, in which a subset of the normal FPSIMD and SVE
  264. features are available. When supported EL0 software may enter and leave
  265. streaming mode at any time.
  266. For best system performance it is strongly encouraged for software to enable
  267. streaming mode only when it is actively being used.
  268. * A new vector length controlling the size of ZA and the Z registers when in
  269. streaming mode, separately to the vector length used for SVE when not in
  270. streaming mode. There is no requirement that either the currently selected
  271. vector length or the set of vector lengths supported for the two modes in
  272. a given system have any relationship. The streaming mode vector length
  273. is referred to as SVL.
  274. * A new ZA matrix register. This is a square matrix of SVLxSVL bits. Most
  275. operations on ZA require that streaming mode be enabled but ZA can be
  276. enabled without streaming mode in order to load, save and retain data.
  277. For best system performance it is strongly encouraged for software to enable
  278. ZA only when it is actively being used.
  279. * Two new 1 bit fields in PSTATE which may be controlled via the SMSTART and
  280. SMSTOP instructions or by access to the SVCR system register:
  281. * PSTATE.ZA, if this is 1 then the ZA matrix is accessible and has valid
  282. data while if it is 0 then ZA can not be accessed. When PSTATE.ZA is
  283. changed from 0 to 1 all bits in ZA are cleared.
  284. * PSTATE.SM, if this is 1 then the PE is in streaming mode. When the value
  285. of PSTATE.SM is changed then it is implementation defined if the subset
  286. of the floating point register bits valid in both modes may be retained.
  287. Any other bits will be cleared.
  288. References
  289. ==========
  290. [1] arch/arm64/include/uapi/asm/sigcontext.h
  291. AArch64 Linux signal ABI definitions
  292. [2] arch/arm64/include/uapi/asm/ptrace.h
  293. AArch64 Linux ptrace ABI definitions
  294. [3] Documentation/arm64/cpu-feature-registers.rst