Skip to content

Commit cd05483

Browse files
AndybnACTpalmer-dabbelt
authored andcommitted
riscv: Allocate user's vector context in the first-use trap
Vector unit is disabled by default for all user processes. Thus, a process will take a trap (illegal instruction) into kernel at the first time when it uses Vector. Only after then, the kernel allocates V context and starts take care of the context for that user process. Suggested-by: Richard Henderson <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Andy Chiu <[email protected]> Reviewed-by: Conor Dooley <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 3a2df63 commit cd05483

File tree

4 files changed

+150
-2
lines changed

4 files changed

+150
-2
lines changed

arch/riscv/include/asm/insn.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,26 @@
137137
#define RVG_OPCODE_JALR 0x67
138138
#define RVG_OPCODE_JAL 0x6f
139139
#define RVG_OPCODE_SYSTEM 0x73
140+
#define RVG_SYSTEM_CSR_OFF 20
141+
#define RVG_SYSTEM_CSR_MASK GENMASK(12, 0)
142+
143+
/* parts of opcode for RVF, RVD and RVQ */
144+
#define RVFDQ_FL_FS_WIDTH_OFF 12
145+
#define RVFDQ_FL_FS_WIDTH_MASK GENMASK(3, 0)
146+
#define RVFDQ_FL_FS_WIDTH_W 2
147+
#define RVFDQ_FL_FS_WIDTH_D 3
148+
#define RVFDQ_LS_FS_WIDTH_Q 4
149+
#define RVFDQ_OPCODE_FL 0x07
150+
#define RVFDQ_OPCODE_FS 0x27
151+
152+
/* parts of opcode for RVV */
153+
#define RVV_OPCODE_VECTOR 0x57
154+
#define RVV_VL_VS_WIDTH_8 0
155+
#define RVV_VL_VS_WIDTH_16 5
156+
#define RVV_VL_VS_WIDTH_32 6
157+
#define RVV_VL_VS_WIDTH_64 7
158+
#define RVV_OPCODE_VL RVFDQ_OPCODE_FL
159+
#define RVV_OPCODE_VS RVFDQ_OPCODE_FS
140160

141161
/* parts of opcode for RVC*/
142162
#define RVC_OPCODE_C0 0x0
@@ -304,6 +324,15 @@ static __always_inline bool riscv_insn_is_branch(u32 code)
304324
(RVC_X(x_, RVC_B_IMM_7_6_OPOFF, RVC_B_IMM_7_6_MASK) << RVC_B_IMM_7_6_OFF) | \
305325
(RVC_IMM_SIGN(x_) << RVC_B_IMM_SIGN_OFF); })
306326

327+
#define RVG_EXTRACT_SYSTEM_CSR(x) \
328+
({typeof(x) x_ = (x); RV_X(x_, RVG_SYSTEM_CSR_OFF, RVG_SYSTEM_CSR_MASK); })
329+
330+
#define RVFDQ_EXTRACT_FL_FS_WIDTH(x) \
331+
({typeof(x) x_ = (x); RV_X(x_, RVFDQ_FL_FS_WIDTH_OFF, \
332+
RVFDQ_FL_FS_WIDTH_MASK); })
333+
334+
#define RVV_EXRACT_VL_VS_WIDTH(x) RVFDQ_EXTRACT_FL_FS_WIDTH(x)
335+
307336
/*
308337
* Get the immediate from a J-type instruction.
309338
*

arch/riscv/include/asm/vector.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
extern unsigned long riscv_v_vsize;
2323
int riscv_v_setup_vsize(void);
24+
bool riscv_v_first_use_handler(struct pt_regs *regs);
2425

2526
static __always_inline bool has_vector(void)
2627
{
@@ -165,6 +166,7 @@ struct pt_regs;
165166

166167
static inline int riscv_v_setup_vsize(void) { return -EOPNOTSUPP; }
167168
static __always_inline bool has_vector(void) { return false; }
169+
static inline bool riscv_v_first_use_handler(struct pt_regs *regs) { return false; }
168170
static inline bool riscv_v_vstate_query(struct pt_regs *regs) { return false; }
169171
#define riscv_v_vsize (0)
170172
#define riscv_v_vstate_save(task, regs) do {} while (0)

arch/riscv/kernel/traps.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <asm/ptrace.h>
2727
#include <asm/syscall.h>
2828
#include <asm/thread_info.h>
29+
#include <asm/vector.h>
2930

3031
int show_unhandled_signals = 1;
3132

@@ -145,8 +146,29 @@ DO_ERROR_INFO(do_trap_insn_misaligned,
145146
SIGBUS, BUS_ADRALN, "instruction address misaligned");
146147
DO_ERROR_INFO(do_trap_insn_fault,
147148
SIGSEGV, SEGV_ACCERR, "instruction access fault");
148-
DO_ERROR_INFO(do_trap_insn_illegal,
149-
SIGILL, ILL_ILLOPC, "illegal instruction");
149+
150+
asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs)
151+
{
152+
if (user_mode(regs)) {
153+
irqentry_enter_from_user_mode(regs);
154+
155+
local_irq_enable();
156+
157+
if (!riscv_v_first_use_handler(regs))
158+
do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
159+
"Oops - illegal instruction");
160+
161+
irqentry_exit_to_user_mode(regs);
162+
} else {
163+
irqentry_state_t state = irqentry_nmi_enter(regs);
164+
165+
do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
166+
"Oops - illegal instruction");
167+
168+
irqentry_nmi_exit(regs, state);
169+
}
170+
}
171+
150172
DO_ERROR_INFO(do_trap_load_fault,
151173
SIGSEGV, SEGV_ACCERR, "load access fault");
152174
#ifndef CONFIG_RISCV_M_MODE

arch/riscv/kernel/vector.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@
44
* Author: Andy Chiu <[email protected]>
55
*/
66
#include <linux/export.h>
7+
#include <linux/sched/signal.h>
8+
#include <linux/types.h>
9+
#include <linux/slab.h>
10+
#include <linux/sched.h>
11+
#include <linux/uaccess.h>
712

13+
#include <asm/thread_info.h>
14+
#include <asm/processor.h>
15+
#include <asm/insn.h>
816
#include <asm/vector.h>
917
#include <asm/csr.h>
1018
#include <asm/elf.h>
19+
#include <asm/ptrace.h>
1120
#include <asm/bug.h>
1221

1322
unsigned long riscv_v_vsize __read_mostly;
@@ -34,3 +43,89 @@ int riscv_v_setup_vsize(void)
3443

3544
return 0;
3645
}
46+
47+
static bool insn_is_vector(u32 insn_buf)
48+
{
49+
u32 opcode = insn_buf & __INSN_OPCODE_MASK;
50+
u32 width, csr;
51+
52+
/*
53+
* All V-related instructions, including CSR operations are 4-Byte. So,
54+
* do not handle if the instruction length is not 4-Byte.
55+
*/
56+
if (unlikely(GET_INSN_LENGTH(insn_buf) != 4))
57+
return false;
58+
59+
switch (opcode) {
60+
case RVV_OPCODE_VECTOR:
61+
return true;
62+
case RVV_OPCODE_VL:
63+
case RVV_OPCODE_VS:
64+
width = RVV_EXRACT_VL_VS_WIDTH(insn_buf);
65+
if (width == RVV_VL_VS_WIDTH_8 || width == RVV_VL_VS_WIDTH_16 ||
66+
width == RVV_VL_VS_WIDTH_32 || width == RVV_VL_VS_WIDTH_64)
67+
return true;
68+
69+
break;
70+
case RVG_OPCODE_SYSTEM:
71+
csr = RVG_EXTRACT_SYSTEM_CSR(insn_buf);
72+
if ((csr >= CSR_VSTART && csr <= CSR_VCSR) ||
73+
(csr >= CSR_VL && csr <= CSR_VLENB))
74+
return true;
75+
}
76+
77+
return false;
78+
}
79+
80+
static int riscv_v_thread_zalloc(void)
81+
{
82+
void *datap;
83+
84+
datap = kzalloc(riscv_v_vsize, GFP_KERNEL);
85+
if (!datap)
86+
return -ENOMEM;
87+
88+
current->thread.vstate.datap = datap;
89+
memset(&current->thread.vstate, 0, offsetof(struct __riscv_v_ext_state,
90+
datap));
91+
return 0;
92+
}
93+
94+
bool riscv_v_first_use_handler(struct pt_regs *regs)
95+
{
96+
u32 __user *epc = (u32 __user *)regs->epc;
97+
u32 insn = (u32)regs->badaddr;
98+
99+
/* Do not handle if V is not supported, or disabled */
100+
if (!(ELF_HWCAP & COMPAT_HWCAP_ISA_V))
101+
return false;
102+
103+
/* If V has been enabled then it is not the first-use trap */
104+
if (riscv_v_vstate_query(regs))
105+
return false;
106+
107+
/* Get the instruction */
108+
if (!insn) {
109+
if (__get_user(insn, epc))
110+
return false;
111+
}
112+
113+
/* Filter out non-V instructions */
114+
if (!insn_is_vector(insn))
115+
return false;
116+
117+
/* Sanity check. datap should be null by the time of the first-use trap */
118+
WARN_ON(current->thread.vstate.datap);
119+
120+
/*
121+
* Now we sure that this is a V instruction. And it executes in the
122+
* context where VS has been off. So, try to allocate the user's V
123+
* context and resume execution.
124+
*/
125+
if (riscv_v_thread_zalloc()) {
126+
force_sig(SIGBUS);
127+
return true;
128+
}
129+
riscv_v_vstate_on(regs);
130+
return true;
131+
}

0 commit comments

Comments
 (0)