vector.rst 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =========================================
  3. Vector Extension Support for RISC-V Linux
  4. =========================================
  5. This document briefly outlines the interface provided to userspace by Linux in
  6. order to support the use of the RISC-V Vector Extension.
  7. 1. prctl() Interface
  8. ---------------------
  9. Two new prctl() calls are added to allow programs to manage the enablement
  10. status for the use of Vector in userspace. The intended usage guideline for
  11. these interfaces is to give init systems a way to modify the availability of V
  12. for processes running under its domain. Calling these interfaces is not
  13. recommended in libraries routines because libraries should not override policies
  14. configured from the parent process. Also, users must note that these interfaces
  15. are not portable to non-Linux, nor non-RISC-V environments, so it is discourage
  16. to use in a portable code. To get the availability of V in an ELF program,
  17. please read :c:macro:`COMPAT_HWCAP_ISA_V` bit of :c:macro:`ELF_HWCAP` in the
  18. auxiliary vector.
  19. * prctl(PR_RISCV_V_SET_CONTROL, unsigned long arg)
  20. Sets the Vector enablement status of the calling thread, where the control
  21. argument consists of two 2-bit enablement statuses and a bit for inheritance
  22. mode. Other threads of the calling process are unaffected.
  23. Enablement status is a tri-state value each occupying 2-bit of space in
  24. the control argument:
  25. * :c:macro:`PR_RISCV_V_VSTATE_CTRL_DEFAULT`: Use the system-wide default
  26. enablement status on execve(). The system-wide default setting can be
  27. controlled via sysctl interface (see sysctl section below).
  28. * :c:macro:`PR_RISCV_V_VSTATE_CTRL_ON`: Allow Vector to be run for the
  29. thread.
  30. * :c:macro:`PR_RISCV_V_VSTATE_CTRL_OFF`: Disallow Vector. Executing Vector
  31. instructions under such condition will trap and casuse the termination of the thread.
  32. arg: The control argument is a 5-bit value consisting of 3 parts, and
  33. accessed by 3 masks respectively.
  34. The 3 masks, PR_RISCV_V_VSTATE_CTRL_CUR_MASK,
  35. PR_RISCV_V_VSTATE_CTRL_NEXT_MASK, and PR_RISCV_V_VSTATE_CTRL_INHERIT
  36. represents bit[1:0], bit[3:2], and bit[4]. bit[1:0] accounts for the
  37. enablement status of current thread, and the setting at bit[3:2] takes place
  38. at next execve(). bit[4] defines the inheritance mode of the setting in
  39. bit[3:2].
  40. * :c:macro:`PR_RISCV_V_VSTATE_CTRL_CUR_MASK`: bit[1:0]: Account for the
  41. Vector enablement status for the calling thread. The calling thread is
  42. not able to turn off Vector once it has been enabled. The prctl() call
  43. fails with EPERM if the value in this mask is PR_RISCV_V_VSTATE_CTRL_OFF
  44. but the current enablement status is not off. Setting
  45. PR_RISCV_V_VSTATE_CTRL_DEFAULT here takes no effect but to set back
  46. the original enablement status.
  47. * :c:macro:`PR_RISCV_V_VSTATE_CTRL_NEXT_MASK`: bit[3:2]: Account for the
  48. Vector enablement setting for the calling thread at the next execve()
  49. system call. If PR_RISCV_V_VSTATE_CTRL_DEFAULT is used in this mask,
  50. then the enablement status will be decided by the system-wide
  51. enablement status when execve() happen.
  52. * :c:macro:`PR_RISCV_V_VSTATE_CTRL_INHERIT`: bit[4]: the inheritance
  53. mode for the setting at PR_RISCV_V_VSTATE_CTRL_NEXT_MASK. If the bit
  54. is set then the following execve() will not clear the setting in both
  55. PR_RISCV_V_VSTATE_CTRL_NEXT_MASK and PR_RISCV_V_VSTATE_CTRL_INHERIT.
  56. This setting persists across changes in the system-wide default value.
  57. Return value:
  58. * 0 on success;
  59. * EINVAL: Vector not supported, invalid enablement status for current or
  60. next mask;
  61. * EPERM: Turning off Vector in PR_RISCV_V_VSTATE_CTRL_CUR_MASK if Vector
  62. was enabled for the calling thread.
  63. On success:
  64. * A valid setting for PR_RISCV_V_VSTATE_CTRL_CUR_MASK takes place
  65. immediately. The enablement status specified in
  66. PR_RISCV_V_VSTATE_CTRL_NEXT_MASK happens at the next execve() call, or
  67. all following execve() calls if PR_RISCV_V_VSTATE_CTRL_INHERIT bit is
  68. set.
  69. * Every successful call overwrites a previous setting for the calling
  70. thread.
  71. * prctl(PR_RISCV_V_GET_CONTROL)
  72. Gets the same Vector enablement status for the calling thread. Setting for
  73. next execve() call and the inheritance bit are all OR-ed together.
  74. Note that ELF programs are able to get the availability of V for itself by
  75. reading :c:macro:`COMPAT_HWCAP_ISA_V` bit of :c:macro:`ELF_HWCAP` in the
  76. auxiliary vector.
  77. Return value:
  78. * a nonnegative value on success;
  79. * EINVAL: Vector not supported.
  80. 2. System runtime configuration (sysctl)
  81. -----------------------------------------
  82. To mitigate the ABI impact of expansion of the signal stack, a
  83. policy mechanism is provided to the administrators, distro maintainers, and
  84. developers to control the default Vector enablement status for userspace
  85. processes in form of sysctl knob:
  86. * /proc/sys/abi/riscv_v_default_allow
  87. Writing the text representation of 0 or 1 to this file sets the default
  88. system enablement status for new starting userspace programs. Valid values
  89. are:
  90. * 0: Do not allow Vector code to be executed as the default for new processes.
  91. * 1: Allow Vector code to be executed as the default for new processes.
  92. Reading this file returns the current system default enablement status.
  93. At every execve() call, a new enablement status of the new process is set to
  94. the system default, unless:
  95. * PR_RISCV_V_VSTATE_CTRL_INHERIT is set for the calling process, and the
  96. setting in PR_RISCV_V_VSTATE_CTRL_NEXT_MASK is not
  97. PR_RISCV_V_VSTATE_CTRL_DEFAULT. Or,
  98. * The setting in PR_RISCV_V_VSTATE_CTRL_NEXT_MASK is not
  99. PR_RISCV_V_VSTATE_CTRL_DEFAULT.
  100. Modifying the system default enablement status does not affect the enablement
  101. status of any existing process of thread that do not make an execve() call.
  102. 3. Vector Register State Across System Calls
  103. ---------------------------------------------
  104. As indicated by version 1.0 of the V extension [1], vector registers are
  105. clobbered by system calls.
  106. 1: https://github.com/riscv/riscv-v-spec/blob/master/calling-convention.adoc