162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci 362306a36Sopenharmony_ci========================================= 462306a36Sopenharmony_ciVector Extension Support for RISC-V Linux 562306a36Sopenharmony_ci========================================= 662306a36Sopenharmony_ci 762306a36Sopenharmony_ciThis document briefly outlines the interface provided to userspace by Linux in 862306a36Sopenharmony_ciorder to support the use of the RISC-V Vector Extension. 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci1. prctl() Interface 1162306a36Sopenharmony_ci--------------------- 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ciTwo new prctl() calls are added to allow programs to manage the enablement 1462306a36Sopenharmony_cistatus for the use of Vector in userspace. The intended usage guideline for 1562306a36Sopenharmony_cithese interfaces is to give init systems a way to modify the availability of V 1662306a36Sopenharmony_cifor processes running under its domain. Calling these interfaces is not 1762306a36Sopenharmony_cirecommended in libraries routines because libraries should not override policies 1862306a36Sopenharmony_ciconfigured from the parant process. Also, users must noted that these interfaces 1962306a36Sopenharmony_ciare not portable to non-Linux, nor non-RISC-V environments, so it is discourage 2062306a36Sopenharmony_cito use in a portable code. To get the availability of V in an ELF program, 2162306a36Sopenharmony_ciplease read :c:macro:`COMPAT_HWCAP_ISA_V` bit of :c:macro:`ELF_HWCAP` in the 2262306a36Sopenharmony_ciauxiliary vector. 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci* prctl(PR_RISCV_V_SET_CONTROL, unsigned long arg) 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci Sets the Vector enablement status of the calling thread, where the control 2762306a36Sopenharmony_ci argument consists of two 2-bit enablement statuses and a bit for inheritance 2862306a36Sopenharmony_ci mode. Other threads of the calling process are unaffected. 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci Enablement status is a tri-state value each occupying 2-bit of space in 3162306a36Sopenharmony_ci the control argument: 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci * :c:macro:`PR_RISCV_V_VSTATE_CTRL_DEFAULT`: Use the system-wide default 3462306a36Sopenharmony_ci enablement status on execve(). The system-wide default setting can be 3562306a36Sopenharmony_ci controlled via sysctl interface (see sysctl section below). 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci * :c:macro:`PR_RISCV_V_VSTATE_CTRL_ON`: Allow Vector to be run for the 3862306a36Sopenharmony_ci thread. 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci * :c:macro:`PR_RISCV_V_VSTATE_CTRL_OFF`: Disallow Vector. Executing Vector 4162306a36Sopenharmony_ci instructions under such condition will trap and casuse the termination of the thread. 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci arg: The control argument is a 5-bit value consisting of 3 parts, and 4462306a36Sopenharmony_ci accessed by 3 masks respectively. 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci The 3 masks, PR_RISCV_V_VSTATE_CTRL_CUR_MASK, 4762306a36Sopenharmony_ci PR_RISCV_V_VSTATE_CTRL_NEXT_MASK, and PR_RISCV_V_VSTATE_CTRL_INHERIT 4862306a36Sopenharmony_ci represents bit[1:0], bit[3:2], and bit[4]. bit[1:0] accounts for the 4962306a36Sopenharmony_ci enablement status of current thread, and the setting at bit[3:2] takes place 5062306a36Sopenharmony_ci at next execve(). bit[4] defines the inheritance mode of the setting in 5162306a36Sopenharmony_ci bit[3:2]. 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci * :c:macro:`PR_RISCV_V_VSTATE_CTRL_CUR_MASK`: bit[1:0]: Account for the 5462306a36Sopenharmony_ci Vector enablement status for the calling thread. The calling thread is 5562306a36Sopenharmony_ci not able to turn off Vector once it has been enabled. The prctl() call 5662306a36Sopenharmony_ci fails with EPERM if the value in this mask is PR_RISCV_V_VSTATE_CTRL_OFF 5762306a36Sopenharmony_ci but the current enablement status is not off. Setting 5862306a36Sopenharmony_ci PR_RISCV_V_VSTATE_CTRL_DEFAULT here takes no effect but to set back 5962306a36Sopenharmony_ci the original enablement status. 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci * :c:macro:`PR_RISCV_V_VSTATE_CTRL_NEXT_MASK`: bit[3:2]: Account for the 6262306a36Sopenharmony_ci Vector enablement setting for the calling thread at the next execve() 6362306a36Sopenharmony_ci system call. If PR_RISCV_V_VSTATE_CTRL_DEFAULT is used in this mask, 6462306a36Sopenharmony_ci then the enablement status will be decided by the system-wide 6562306a36Sopenharmony_ci enablement status when execve() happen. 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci * :c:macro:`PR_RISCV_V_VSTATE_CTRL_INHERIT`: bit[4]: the inheritance 6862306a36Sopenharmony_ci mode for the setting at PR_RISCV_V_VSTATE_CTRL_NEXT_MASK. If the bit 6962306a36Sopenharmony_ci is set then the following execve() will not clear the setting in both 7062306a36Sopenharmony_ci PR_RISCV_V_VSTATE_CTRL_NEXT_MASK and PR_RISCV_V_VSTATE_CTRL_INHERIT. 7162306a36Sopenharmony_ci This setting persists across changes in the system-wide default value. 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci Return value: 7462306a36Sopenharmony_ci * 0 on success; 7562306a36Sopenharmony_ci * EINVAL: Vector not supported, invalid enablement status for current or 7662306a36Sopenharmony_ci next mask; 7762306a36Sopenharmony_ci * EPERM: Turning off Vector in PR_RISCV_V_VSTATE_CTRL_CUR_MASK if Vector 7862306a36Sopenharmony_ci was enabled for the calling thread. 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci On success: 8162306a36Sopenharmony_ci * A valid setting for PR_RISCV_V_VSTATE_CTRL_CUR_MASK takes place 8262306a36Sopenharmony_ci immediately. The enablement status specified in 8362306a36Sopenharmony_ci PR_RISCV_V_VSTATE_CTRL_NEXT_MASK happens at the next execve() call, or 8462306a36Sopenharmony_ci all following execve() calls if PR_RISCV_V_VSTATE_CTRL_INHERIT bit is 8562306a36Sopenharmony_ci set. 8662306a36Sopenharmony_ci * Every successful call overwrites a previous setting for the calling 8762306a36Sopenharmony_ci thread. 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci* prctl(PR_RISCV_V_GET_CONTROL) 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci Gets the same Vector enablement status for the calling thread. Setting for 9262306a36Sopenharmony_ci next execve() call and the inheritance bit are all OR-ed together. 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci Note that ELF programs are able to get the availability of V for itself by 9562306a36Sopenharmony_ci reading :c:macro:`COMPAT_HWCAP_ISA_V` bit of :c:macro:`ELF_HWCAP` in the 9662306a36Sopenharmony_ci auxiliary vector. 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci Return value: 9962306a36Sopenharmony_ci * a nonnegative value on success; 10062306a36Sopenharmony_ci * EINVAL: Vector not supported. 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci2. System runtime configuration (sysctl) 10362306a36Sopenharmony_ci----------------------------------------- 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ciTo mitigate the ABI impact of expansion of the signal stack, a 10662306a36Sopenharmony_cipolicy mechanism is provided to the administrators, distro maintainers, and 10762306a36Sopenharmony_cidevelopers to control the default Vector enablement status for userspace 10862306a36Sopenharmony_ciprocesses in form of sysctl knob: 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci* /proc/sys/abi/riscv_v_default_allow 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci Writing the text representation of 0 or 1 to this file sets the default 11362306a36Sopenharmony_ci system enablement status for new starting userspace programs. Valid values 11462306a36Sopenharmony_ci are: 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci * 0: Do not allow Vector code to be executed as the default for new processes. 11762306a36Sopenharmony_ci * 1: Allow Vector code to be executed as the default for new processes. 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ci Reading this file returns the current system default enablement status. 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci At every execve() call, a new enablement status of the new process is set to 12262306a36Sopenharmony_ci the system default, unless: 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci * PR_RISCV_V_VSTATE_CTRL_INHERIT is set for the calling process, and the 12562306a36Sopenharmony_ci setting in PR_RISCV_V_VSTATE_CTRL_NEXT_MASK is not 12662306a36Sopenharmony_ci PR_RISCV_V_VSTATE_CTRL_DEFAULT. Or, 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci * The setting in PR_RISCV_V_VSTATE_CTRL_NEXT_MASK is not 12962306a36Sopenharmony_ci PR_RISCV_V_VSTATE_CTRL_DEFAULT. 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci Modifying the system default enablement status does not affect the enablement 13262306a36Sopenharmony_ci status of any existing process of thread that do not make an execve() call. 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci3. Vector Register State Across System Calls 13562306a36Sopenharmony_ci--------------------------------------------- 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ciAs indicated by version 1.0 of the V extension [1], vector registers are 13862306a36Sopenharmony_ciclobbered by system calls. 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci1: https://github.com/riscv/riscv-v-spec/blob/master/calling-convention.adoc 141