Skip to content

Commit 280f11f

Browse files
clementlegerjfvogel
authored andcommitted
riscv: misaligned: factorize trap handling
[ Upstream commit fd94de9f9e7aac11ec659e386b9db1203d502023 ] Since both load/store and user/kernel should use almost the same path and that we are going to add some code around that, factorize it. Signed-off-by: Clément Léger <cleger@rivosinc.com> Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com> Link: https://lore.kernel.org/r/20250422162324.956065-2-cleger@rivosinc.com Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com> Stable-dep-of: 453805f0a28f ("riscv: misaligned: enable IRQs while handling misaligned accesses") Signed-off-by: Sasha Levin <sashal@kernel.org> (cherry picked from commit 45a0697ceeae242832b075da7aa4918417410563) Signed-off-by: Jack Vogel <jack.vogel@oracle.com>
1 parent 1648564 commit 280f11f

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

arch/riscv/kernel/traps.c

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -198,47 +198,53 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re
198198
DO_ERROR_INFO(do_trap_load_fault,
199199
SIGSEGV, SEGV_ACCERR, "load access fault");
200200

201-
asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs)
201+
enum misaligned_access_type {
202+
MISALIGNED_STORE,
203+
MISALIGNED_LOAD,
204+
};
205+
static const struct {
206+
const char *type_str;
207+
int (*handler)(struct pt_regs *regs);
208+
} misaligned_handler[] = {
209+
[MISALIGNED_STORE] = {
210+
.type_str = "Oops - store (or AMO) address misaligned",
211+
.handler = handle_misaligned_store,
212+
},
213+
[MISALIGNED_LOAD] = {
214+
.type_str = "Oops - load address misaligned",
215+
.handler = handle_misaligned_load,
216+
},
217+
};
218+
219+
static void do_trap_misaligned(struct pt_regs *regs, enum misaligned_access_type type)
202220
{
203-
if (user_mode(regs)) {
221+
irqentry_state_t state;
222+
223+
if (user_mode(regs))
204224
irqentry_enter_from_user_mode(regs);
225+
else
226+
state = irqentry_nmi_enter(regs);
205227

206-
if (handle_misaligned_load(regs))
207-
do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
208-
"Oops - load address misaligned");
228+
if (misaligned_handler[type].handler(regs))
229+
do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
230+
misaligned_handler[type].type_str);
209231

232+
if (user_mode(regs))
210233
irqentry_exit_to_user_mode(regs);
211-
} else {
212-
irqentry_state_t state = irqentry_nmi_enter(regs);
213-
214-
if (handle_misaligned_load(regs))
215-
do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
216-
"Oops - load address misaligned");
217-
234+
else
218235
irqentry_nmi_exit(regs, state);
219-
}
220236
}
221237

222-
asmlinkage __visible __trap_section void do_trap_store_misaligned(struct pt_regs *regs)
238+
asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs)
223239
{
224-
if (user_mode(regs)) {
225-
irqentry_enter_from_user_mode(regs);
226-
227-
if (handle_misaligned_store(regs))
228-
do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
229-
"Oops - store (or AMO) address misaligned");
230-
231-
irqentry_exit_to_user_mode(regs);
232-
} else {
233-
irqentry_state_t state = irqentry_nmi_enter(regs);
234-
235-
if (handle_misaligned_store(regs))
236-
do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
237-
"Oops - store (or AMO) address misaligned");
240+
do_trap_misaligned(regs, MISALIGNED_LOAD);
241+
}
238242

239-
irqentry_nmi_exit(regs, state);
240-
}
243+
asmlinkage __visible __trap_section void do_trap_store_misaligned(struct pt_regs *regs)
244+
{
245+
do_trap_misaligned(regs, MISALIGNED_STORE);
241246
}
247+
242248
DO_ERROR_INFO(do_trap_store_fault,
243249
SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
244250
DO_ERROR_INFO(do_trap_ecall_s,

0 commit comments

Comments
 (0)