Skip to content

Commit 775acc8

Browse files
kirylhansendc
authored andcommitted
x86/traps: Refactor exc_general_protection()
TDX brings a new exception -- Virtualization Exception (#VE). Handling of #VE structurally very similar to handling #GP. Extract two helpers from exc_general_protection() that can be reused for handling #VE. No functional changes. Suggested-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Reviewed-by: Dave Hansen <dave.hansen@linux.intel.com> Link: https://lkml.kernel.org/r/20220405232939.73860-7-kirill.shutemov@linux.intel.com
1 parent 65fab5b commit 775acc8

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

arch/x86/kernel/traps.c

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -686,13 +686,40 @@ static bool try_fixup_enqcmd_gp(void)
686686
#endif
687687
}
688688

689+
static bool gp_try_fixup_and_notify(struct pt_regs *regs, int trapnr,
690+
unsigned long error_code, const char *str)
691+
{
692+
if (fixup_exception(regs, trapnr, error_code, 0))
693+
return true;
694+
695+
current->thread.error_code = error_code;
696+
current->thread.trap_nr = trapnr;
697+
698+
/*
699+
* To be potentially processing a kprobe fault and to trust the result
700+
* from kprobe_running(), we have to be non-preemptible.
701+
*/
702+
if (!preemptible() && kprobe_running() &&
703+
kprobe_fault_handler(regs, trapnr))
704+
return true;
705+
706+
return notify_die(DIE_GPF, str, regs, error_code, trapnr, SIGSEGV) == NOTIFY_STOP;
707+
}
708+
709+
static void gp_user_force_sig_segv(struct pt_regs *regs, int trapnr,
710+
unsigned long error_code, const char *str)
711+
{
712+
current->thread.error_code = error_code;
713+
current->thread.trap_nr = trapnr;
714+
show_signal(current, SIGSEGV, "", str, regs, error_code);
715+
force_sig(SIGSEGV);
716+
}
717+
689718
DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
690719
{
691720
char desc[sizeof(GPFSTR) + 50 + 2*sizeof(unsigned long) + 1] = GPFSTR;
692721
enum kernel_gp_hint hint = GP_NO_HINT;
693-
struct task_struct *tsk;
694722
unsigned long gp_addr;
695-
int ret;
696723

697724
if (user_mode(regs) && try_fixup_enqcmd_gp())
698725
return;
@@ -711,40 +738,18 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
711738
return;
712739
}
713740

714-
tsk = current;
715-
716741
if (user_mode(regs)) {
717742
if (fixup_iopl_exception(regs))
718743
goto exit;
719744

720-
tsk->thread.error_code = error_code;
721-
tsk->thread.trap_nr = X86_TRAP_GP;
722-
723745
if (fixup_vdso_exception(regs, X86_TRAP_GP, error_code, 0))
724746
goto exit;
725747

726-
show_signal(tsk, SIGSEGV, "", desc, regs, error_code);
727-
force_sig(SIGSEGV);
748+
gp_user_force_sig_segv(regs, X86_TRAP_GP, error_code, desc);
728749
goto exit;
729750
}
730751

731-
if (fixup_exception(regs, X86_TRAP_GP, error_code, 0))
732-
goto exit;
733-
734-
tsk->thread.error_code = error_code;
735-
tsk->thread.trap_nr = X86_TRAP_GP;
736-
737-
/*
738-
* To be potentially processing a kprobe fault and to trust the result
739-
* from kprobe_running(), we have to be non-preemptible.
740-
*/
741-
if (!preemptible() &&
742-
kprobe_running() &&
743-
kprobe_fault_handler(regs, X86_TRAP_GP))
744-
goto exit;
745-
746-
ret = notify_die(DIE_GPF, desc, regs, error_code, X86_TRAP_GP, SIGSEGV);
747-
if (ret == NOTIFY_STOP)
752+
if (gp_try_fixup_and_notify(regs, X86_TRAP_GP, error_code, desc))
748753
goto exit;
749754

750755
if (error_code)

0 commit comments

Comments
 (0)