sme.rst 18 KB

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