Skip to content
Permalink
Browse files
x86/entry: Defer paranoid entry/exit to C code
IST entries from the kernel use paranoid entry and exit
assembly functions to ensure the CR3 and GS registers are
updated with correct values for the kernel. Move the update
of the CR3 and GS registers inside the C code of IST handlers.

Signed-off-by: Alexandre Chartre <alexandre.chartre@oracle.com>
  • Loading branch information
achartre authored and intel-lab-lkp committed Nov 9, 2020
1 parent 1095b6a commit e89fc37752f2832fa239ef9e40c6c089e204561a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 60 deletions.
@@ -462,16 +462,16 @@ SYM_CODE_START(\asmsym)
/* Entry from kernel */

pushq $-1 /* ORIG_RAX: no syscall to restart */
/* paranoid_entry returns GS information for paranoid_exit in EBX. */
call paranoid_entry

cld
PUSH_AND_CLEAR_REGS
ENCODE_FRAME_POINTER
UNWIND_HINT_REGS

movq %rsp, %rdi /* pt_regs pointer */

call \cfunc

jmp paranoid_exit
jmp restore_regs_and_return_to_kernel

_ASM_NOKPROBE(\asmsym)
SYM_CODE_END(\asmsym)
@@ -507,12 +507,9 @@ SYM_CODE_START(\asmsym)
*/
ist_entry_user safe_stack_\cfunc, has_error_code=1

/*
* paranoid_entry returns SWAPGS flag for paranoid_exit in EBX.
* EBX == 0 -> SWAPGS, EBX == 1 -> no SWAPGS
*/
call paranoid_entry

cld
PUSH_AND_CLEAR_REGS
ENCODE_FRAME_POINTER
UNWIND_HINT_REGS

/*
@@ -538,7 +535,7 @@ SYM_CODE_START(\asmsym)
* identical to the stack in the IRET frame or the VC fall-back stack,
* so it is definitly mapped even with PTI enabled.
*/
jmp paranoid_exit
jmp restore_regs_and_return_to_kernel

_ASM_NOKPROBE(\asmsym)
SYM_CODE_END(\asmsym)
@@ -555,16 +552,17 @@ SYM_CODE_START(\asmsym)
UNWIND_HINT_IRET_REGS offset=8
ASM_CLAC

/* paranoid_entry returns GS information for paranoid_exit in EBX. */
call paranoid_entry
cld
PUSH_AND_CLEAR_REGS
ENCODE_FRAME_POINTER
UNWIND_HINT_REGS

movq %rsp, %rdi /* pt_regs pointer into first argument */
movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/
movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
call \cfunc

jmp paranoid_exit
jmp restore_regs_and_return_to_kernel

_ASM_NOKPROBE(\asmsym)
SYM_CODE_END(\asmsym)
@@ -1119,10 +1117,6 @@ SYM_CODE_END(error_return)
/*
* Runs on exception stack. Xen PV does not go through this path at all,
* so we can use real assembly here.
*
* Registers:
* %r14: Used to save/restore the CR3 of the interrupted context
* when PAGE_TABLE_ISOLATION is in use. Do not clobber.
*/
SYM_CODE_START(asm_exc_nmi)
/*
@@ -1173,7 +1167,7 @@ SYM_CODE_START(asm_exc_nmi)
* We also must not push anything to the stack before switching
* stacks lest we corrupt the "NMI executing" variable.
*/
ist_entry_user exc_nmi
ist_entry_user exc_nmi_user

/* NMI from kernel */

@@ -1346,9 +1340,7 @@ repeat_nmi:
*
* RSP is pointing to "outermost RIP". gsbase is unknown, but, if
* we're repeating an NMI, gsbase has the same value that it had on
* the first iteration. paranoid_entry will load the kernel
* gsbase if needed before we call exc_nmi(). "NMI executing"
* is zero.
* the first iteration. "NMI executing" is zero.
*/
movq $1, 10*8(%rsp) /* Set "NMI executing". */

@@ -1372,44 +1364,20 @@ end_repeat_nmi:
pushq $-1 /* ORIG_RAX: no syscall to restart */

/*
* Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
* as we should not be calling schedule in NMI context.
* Even with normal interrupts enabled. An NMI should not be
* setting NEED_RESCHED or anything that normal interrupts and
* We should not be calling schedule in NMI context. Even with
* normal interrupts enabled. An NMI should not be setting
* NEED_RESCHED or anything that normal interrupts and
* exceptions might do.
*/
call paranoid_entry
cld
PUSH_AND_CLEAR_REGS
ENCODE_FRAME_POINTER
UNWIND_HINT_REGS

movq %rsp, %rdi
movq $-1, %rsi
call exc_nmi

/* Always restore stashed CR3 value (see paranoid_entry) */
RESTORE_CR3 scratch_reg=%r15 save_reg=%r14

/*
* The above invocation of paranoid_entry stored the GSBASE
* related information in R/EBX depending on the availability
* of FSGSBASE.
*
* If FSGSBASE is enabled, restore the saved GSBASE value
* unconditionally, otherwise take the conditional SWAPGS path.
*/
ALTERNATIVE "jmp nmi_no_fsgsbase", "", X86_FEATURE_FSGSBASE

wrgsbase %rbx
jmp nmi_restore

nmi_no_fsgsbase:
/* EBX == 0 -> invoke SWAPGS */
testl %ebx, %ebx
jnz nmi_restore

nmi_swapgs:
SWAPGS_UNSAFE_STACK

nmi_restore:
POP_REGS

/*
@@ -2022,11 +2022,14 @@ static __always_inline void exc_machine_check_user(struct pt_regs *regs)
/* MCE hit kernel mode */
DEFINE_IDTENTRY_MCE(exc_machine_check)
{
struct kernel_entry_state entry_state;
unsigned long dr7;

kernel_paranoid_entry(&entry_state);
dr7 = local_db_save();
exc_machine_check_kernel(regs);
local_db_restore(dr7);
kernel_paranoid_exit(&entry_state);
}

/* The user mode variant. */
@@ -473,7 +473,7 @@ static DEFINE_PER_CPU(enum nmi_states, nmi_state);
static DEFINE_PER_CPU(unsigned long, nmi_cr2);
static DEFINE_PER_CPU(unsigned long, nmi_dr7);

DEFINE_IDTENTRY_NMI(exc_nmi)
static noinstr void handle_nmi(struct pt_regs *regs)
{
bool irq_state;

@@ -529,9 +529,21 @@ DEFINE_IDTENTRY_NMI(exc_nmi)
write_cr2(this_cpu_read(nmi_cr2));
if (this_cpu_dec_return(nmi_state))
goto nmi_restart;
}

DEFINE_IDTENTRY_NMI(exc_nmi)
{
struct kernel_entry_state entry_state;

kernel_paranoid_entry(&entry_state);
handle_nmi(regs);
kernel_paranoid_exit(&entry_state);
}

if (user_mode(regs))
mds_user_clear_cpu_buffers();
__visible noinstr void exc_nmi_user(struct pt_regs *regs)
{
handle_nmi(regs);
mds_user_clear_cpu_buffers();
}

void stop_nmi(void)
@@ -1346,13 +1346,25 @@ DEFINE_IDTENTRY_VC_IST(exc_vmm_communication)
struct exc_vc_frame {
/* pt_regs should be first */
struct pt_regs regs;
/* extra parameters for the handler */
struct kernel_entry_state entry_state;
};

DEFINE_IDTENTRY_VC_SETUP_STACK(exc_vmm_communication)
{
struct kernel_entry_state entry_state;
struct exc_vc_frame *frame;
unsigned long sp;

/*
* kernel_paranoid_entry() is called first to properly set
* the GS register which is used to access per-cpu variables.
*
* vc_switch_off_ist() uses per-cpu variables so it has to be
* called after kernel_paranoid_entry().
*/
kernel_paranoid_entry(&entry_state);

/*
* Switch off the IST stack to make it free for nested exceptions.
* The vc_switch_off_ist() function will switch back to the
@@ -1364,7 +1376,8 @@ DEFINE_IDTENTRY_VC_SETUP_STACK(exc_vmm_communication)
/*
* Found a safe stack. Set it up as if the entry has happened on
* that stack. This means that we need to have pt_regs at the top
* of the stack.
* of the stack, and we can use the bottom of the stack to pass
* extra parameters (like the kernel entry state) to the handler.
*
* The effective stack switch happens in assembly code before
* the #VC handler is called.
@@ -1373,16 +1386,21 @@ DEFINE_IDTENTRY_VC_SETUP_STACK(exc_vmm_communication)

frame = (struct exc_vc_frame *)sp;
frame->regs = *regs;
frame->entry_state = entry_state;

return sp;
}

DEFINE_IDTENTRY_VC(exc_vmm_communication)
{
struct exc_vc_frame *frame = (struct exc_vc_frame *)regs;

if (likely(!on_vc_fallback_stack(regs)))
safe_stack_exc_vmm_communication(regs, error_code);
else
ist_exc_vmm_communication(regs, error_code);

kernel_paranoid_exit(&frame->entry_state);
}

bool __init handle_vc_boot_ghcb(struct pt_regs *regs)
@@ -344,10 +344,10 @@ __visible void __noreturn handle_stack_overflow(const char *message,
DEFINE_IDTENTRY_DF(exc_double_fault)
{
static const char str[] = "double fault";
struct task_struct *tsk = current;

struct task_struct *tsk;
struct kernel_entry_state entry_state;
#ifdef CONFIG_VMAP_STACK
unsigned long address = read_cr2();
unsigned long address;
#endif

#ifdef CONFIG_X86_ESPFIX64
@@ -371,8 +371,12 @@ DEFINE_IDTENTRY_DF(exc_double_fault)
regs->cs == __KERNEL_CS &&
regs->ip == (unsigned long)native_irq_return_iret)
{
struct pt_regs *gpregs = (struct pt_regs *)this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
unsigned long *p = (unsigned long *)regs->sp;
struct pt_regs *gpregs;

kernel_paranoid_entry(&entry_state);

gpregs = (struct pt_regs *)this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;

/*
* regs->sp points to the failing IRET frame on the
@@ -401,14 +405,28 @@ DEFINE_IDTENTRY_DF(exc_double_fault)
regs->ip = (unsigned long)asm_exc_general_protection;
regs->sp = (unsigned long)&gpregs->orig_ax;

kernel_paranoid_exit(&entry_state);

return;
}
#endif

/*
* Switch to the kernel page-table. We are on an IST stack, and
* we are going to die so there is no need to switch to the kernel
* stack even if we are coming from userspace.
*/
kernel_paranoid_entry(&entry_state);

#ifdef CONFIG_VMAP_STACK
address = read_cr2();
#endif

idtentry_enter_nmi(regs);
instrumentation_begin();
notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);

tsk = current;
tsk->thread.error_code = error_code;
tsk->thread.trap_nr = X86_TRAP_DF;

@@ -956,7 +974,11 @@ static __always_inline void exc_debug_user(struct pt_regs *regs,
/* IST stack entry */
DEFINE_IDTENTRY_DEBUG(exc_debug)
{
struct kernel_entry_state entry_state;

kernel_paranoid_entry(&entry_state);
exc_debug_kernel(regs, debug_read_clear_dr6());
kernel_paranoid_exit(&entry_state);
}

/* User entry, runs on regular task stack */

0 comments on commit e89fc37

Please sign in to comment.