Skip to content

Commit 27d6b4d

Browse files
committed
x86/entry: Use generic syscall entry function
Replace the syscall entry work handling with the generic version. Provide the necessary helper inlines to handle the real architecture specific parts, e.g. ptrace. Use a temporary define for idtentry_enter_user which will be cleaned up seperately. Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Kees Cook <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 0bf019e commit 27d6b4d

File tree

5 files changed

+45
-179
lines changed

5 files changed

+45
-179
lines changed

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ config X86
115115
select GENERIC_CPU_AUTOPROBE
116116
select GENERIC_CPU_VULNERABILITIES
117117
select GENERIC_EARLY_IOREMAP
118+
select GENERIC_ENTRY
118119
select GENERIC_FIND_FIRST_BIT
119120
select GENERIC_IOMAP
120121
select GENERIC_IRQ_EFFECTIVE_AFF_MASK if SMP

arch/x86/entry/common.c

Lines changed: 8 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
#include <linux/kernel.h>
1111
#include <linux/sched.h>
1212
#include <linux/sched/task_stack.h>
13+
#include <linux/entry-common.h>
1314
#include <linux/mm.h>
1415
#include <linux/smp.h>
1516
#include <linux/errno.h>
1617
#include <linux/ptrace.h>
1718
#include <linux/tracehook.h>
1819
#include <linux/audit.h>
19-
#include <linux/seccomp.h>
2020
#include <linux/signal.h>
2121
#include <linux/export.h>
2222
#include <linux/context_tracking.h>
@@ -42,70 +42,8 @@
4242
#include <asm/syscall.h>
4343
#include <asm/irq_stack.h>
4444

45-
#define CREATE_TRACE_POINTS
4645
#include <trace/events/syscalls.h>
4746

48-
/* Check that the stack and regs on entry from user mode are sane. */
49-
static noinstr void check_user_regs(struct pt_regs *regs)
50-
{
51-
if (IS_ENABLED(CONFIG_DEBUG_ENTRY)) {
52-
/*
53-
* Make sure that the entry code gave us a sensible EFLAGS
54-
* register. Native because we want to check the actual CPU
55-
* state, not the interrupt state as imagined by Xen.
56-
*/
57-
unsigned long flags = native_save_fl();
58-
WARN_ON_ONCE(flags & (X86_EFLAGS_AC | X86_EFLAGS_DF |
59-
X86_EFLAGS_NT));
60-
61-
/* We think we came from user mode. Make sure pt_regs agrees. */
62-
WARN_ON_ONCE(!user_mode(regs));
63-
64-
/*
65-
* All entries from user mode (except #DF) should be on the
66-
* normal thread stack and should have user pt_regs in the
67-
* correct location.
68-
*/
69-
WARN_ON_ONCE(!on_thread_stack());
70-
WARN_ON_ONCE(regs != task_pt_regs(current));
71-
}
72-
}
73-
74-
#ifdef CONFIG_CONTEXT_TRACKING
75-
/**
76-
* enter_from_user_mode - Establish state when coming from user mode
77-
*
78-
* Syscall entry disables interrupts, but user mode is traced as interrupts
79-
* enabled. Also with NO_HZ_FULL RCU might be idle.
80-
*
81-
* 1) Tell lockdep that interrupts are disabled
82-
* 2) Invoke context tracking if enabled to reactivate RCU
83-
* 3) Trace interrupts off state
84-
*/
85-
static noinstr void enter_from_user_mode(struct pt_regs *regs)
86-
{
87-
enum ctx_state state = ct_state();
88-
89-
check_user_regs(regs);
90-
lockdep_hardirqs_off(CALLER_ADDR0);
91-
user_exit_irqoff();
92-
93-
instrumentation_begin();
94-
CT_WARN_ON(state != CONTEXT_USER);
95-
trace_hardirqs_off_finish();
96-
instrumentation_end();
97-
}
98-
#else
99-
static __always_inline void enter_from_user_mode(struct pt_regs *regs)
100-
{
101-
check_user_regs(regs);
102-
lockdep_hardirqs_off(CALLER_ADDR0);
103-
instrumentation_begin();
104-
trace_hardirqs_off_finish();
105-
instrumentation_end();
106-
}
107-
#endif
108-
10947
/**
11048
* exit_to_user_mode - Fixup state when exiting to user mode
11149
*
@@ -129,83 +67,6 @@ static __always_inline void exit_to_user_mode(void)
12967
lockdep_hardirqs_on(CALLER_ADDR0);
13068
}
13169

132-
static void do_audit_syscall_entry(struct pt_regs *regs, u32 arch)
133-
{
134-
#ifdef CONFIG_X86_64
135-
if (arch == AUDIT_ARCH_X86_64) {
136-
audit_syscall_entry(regs->orig_ax, regs->di,
137-
regs->si, regs->dx, regs->r10);
138-
} else
139-
#endif
140-
{
141-
audit_syscall_entry(regs->orig_ax, regs->bx,
142-
regs->cx, regs->dx, regs->si);
143-
}
144-
}
145-
146-
/*
147-
* Returns the syscall nr to run (which should match regs->orig_ax) or -1
148-
* to skip the syscall.
149-
*/
150-
static long syscall_trace_enter(struct pt_regs *regs)
151-
{
152-
u32 arch = in_ia32_syscall() ? AUDIT_ARCH_I386 : AUDIT_ARCH_X86_64;
153-
154-
struct thread_info *ti = current_thread_info();
155-
unsigned long ret = 0;
156-
u32 work;
157-
158-
work = READ_ONCE(ti->flags);
159-
160-
if (work & (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_EMU)) {
161-
ret = tracehook_report_syscall_entry(regs);
162-
if (ret || (work & _TIF_SYSCALL_EMU))
163-
return -1L;
164-
}
165-
166-
#ifdef CONFIG_SECCOMP
167-
/*
168-
* Do seccomp after ptrace, to catch any tracer changes.
169-
*/
170-
if (work & _TIF_SECCOMP) {
171-
struct seccomp_data sd;
172-
173-
sd.arch = arch;
174-
sd.nr = regs->orig_ax;
175-
sd.instruction_pointer = regs->ip;
176-
#ifdef CONFIG_X86_64
177-
if (arch == AUDIT_ARCH_X86_64) {
178-
sd.args[0] = regs->di;
179-
sd.args[1] = regs->si;
180-
sd.args[2] = regs->dx;
181-
sd.args[3] = regs->r10;
182-
sd.args[4] = regs->r8;
183-
sd.args[5] = regs->r9;
184-
} else
185-
#endif
186-
{
187-
sd.args[0] = regs->bx;
188-
sd.args[1] = regs->cx;
189-
sd.args[2] = regs->dx;
190-
sd.args[3] = regs->si;
191-
sd.args[4] = regs->di;
192-
sd.args[5] = regs->bp;
193-
}
194-
195-
ret = __secure_computing(&sd);
196-
if (ret == -1)
197-
return ret;
198-
}
199-
#endif
200-
201-
if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
202-
trace_sys_enter(regs, regs->orig_ax);
203-
204-
do_audit_syscall_entry(regs, arch);
205-
206-
return ret ?: regs->orig_ax;
207-
}
208-
20970
#define EXIT_TO_USERMODE_LOOP_FLAGS \
21071
(_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
21172
_TIF_NEED_RESCHED | _TIF_PATCH_PENDING)
@@ -366,26 +227,10 @@ __visible noinstr void syscall_return_slowpath(struct pt_regs *regs)
366227
exit_to_user_mode();
367228
}
368229

369-
static noinstr long syscall_enter(struct pt_regs *regs, unsigned long nr)
370-
{
371-
struct thread_info *ti;
372-
373-
enter_from_user_mode(regs);
374-
instrumentation_begin();
375-
376-
local_irq_enable();
377-
ti = current_thread_info();
378-
if (READ_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY)
379-
nr = syscall_trace_enter(regs);
380-
381-
instrumentation_end();
382-
return nr;
383-
}
384-
385230
#ifdef CONFIG_X86_64
386231
__visible noinstr void do_syscall_64(unsigned long nr, struct pt_regs *regs)
387232
{
388-
nr = syscall_enter(regs, nr);
233+
nr = syscall_enter_from_user_mode(regs, nr);
389234

390235
instrumentation_begin();
391236
if (likely(nr < NR_syscalls)) {
@@ -407,14 +252,16 @@ __visible noinstr void do_syscall_64(unsigned long nr, struct pt_regs *regs)
407252
#if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
408253
static __always_inline unsigned int syscall_32_enter(struct pt_regs *regs)
409254
{
255+
unsigned int nr = (unsigned int)regs->orig_ax;
256+
410257
if (IS_ENABLED(CONFIG_IA32_EMULATION))
411258
current_thread_info()->status |= TS_COMPAT;
412259
/*
413260
* Subtlety here: if ptrace pokes something larger than 2^32-1 into
414261
* orig_ax, the unsigned int return value truncates it. This may
415262
* or may not be necessary, but it matches the old asm behavior.
416263
*/
417-
return syscall_enter(regs, (unsigned int)regs->orig_ax);
264+
return (unsigned int)syscall_enter_from_user_mode(regs, nr);
418265
}
419266

420267
/*
@@ -568,7 +415,7 @@ SYSCALL_DEFINE0(ni_syscall)
568415
* solves the problem of kernel mode pagefaults which can schedule, which
569416
* is not possible after invoking rcu_irq_enter() without undoing it.
570417
*
571-
* For user mode entries enter_from_user_mode() must be invoked to
418+
* For user mode entries irqentry_enter_from_user_mode() must be invoked to
572419
* establish the proper context for NOHZ_FULL. Otherwise scheduling on exit
573420
* would not be possible.
574421
*
@@ -584,7 +431,7 @@ idtentry_state_t noinstr idtentry_enter(struct pt_regs *regs)
584431
};
585432

586433
if (user_mode(regs)) {
587-
enter_from_user_mode(regs);
434+
irqentry_enter_from_user_mode(regs);
588435
return ret;
589436
}
590437

@@ -615,7 +462,7 @@ idtentry_state_t noinstr idtentry_enter(struct pt_regs *regs)
615462
/*
616463
* If RCU is not watching then the same careful
617464
* sequence vs. lockdep and tracing is required
618-
* as in enter_from_user_mode().
465+
* as in irqentry_enter_from_user_mode().
619466
*/
620467
lockdep_hardirqs_off(CALLER_ADDR0);
621468
rcu_irq_enter();
@@ -708,18 +555,6 @@ void noinstr idtentry_exit(struct pt_regs *regs, idtentry_state_t state)
708555
}
709556
}
710557

711-
/**
712-
* idtentry_enter_user - Handle state tracking on idtentry from user mode
713-
* @regs: Pointer to pt_regs of interrupted context
714-
*
715-
* Invokes enter_from_user_mode() to establish the proper context for
716-
* NOHZ_FULL. Otherwise scheduling on exit would not be possible.
717-
*/
718-
void noinstr idtentry_enter_user(struct pt_regs *regs)
719-
{
720-
enter_from_user_mode(regs);
721-
}
722-
723558
/**
724559
* idtentry_exit_user - Handle return from exception to user mode
725560
* @regs: Pointer to pt_regs (exception entry regs)

arch/x86/include/asm/entry-common.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
#ifndef _ASM_X86_ENTRY_COMMON_H
3+
#define _ASM_X86_ENTRY_COMMON_H
4+
5+
/* Check that the stack and regs on entry from user mode are sane. */
6+
static __always_inline void arch_check_user_regs(struct pt_regs *regs)
7+
{
8+
if (IS_ENABLED(CONFIG_DEBUG_ENTRY)) {
9+
/*
10+
* Make sure that the entry code gave us a sensible EFLAGS
11+
* register. Native because we want to check the actual CPU
12+
* state, not the interrupt state as imagined by Xen.
13+
*/
14+
unsigned long flags = native_save_fl();
15+
WARN_ON_ONCE(flags & (X86_EFLAGS_AC | X86_EFLAGS_DF |
16+
X86_EFLAGS_NT));
17+
18+
/* We think we came from user mode. Make sure pt_regs agrees. */
19+
WARN_ON_ONCE(!user_mode(regs));
20+
21+
/*
22+
* All entries from user mode (except #DF) should be on the
23+
* normal thread stack and should have user pt_regs in the
24+
* correct location.
25+
*/
26+
WARN_ON_ONCE(!on_thread_stack());
27+
WARN_ON_ONCE(regs != task_pt_regs(current));
28+
}
29+
}
30+
#define arch_check_user_regs arch_check_user_regs
31+
32+
#endif

arch/x86/include/asm/idtentry.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
#include <asm/trapnr.h>
77

88
#ifndef __ASSEMBLY__
9+
#include <linux/entry-common.h>
910
#include <linux/hardirq.h>
1011

1112
#include <asm/irq_stack.h>
1213

13-
void idtentry_enter_user(struct pt_regs *regs);
14+
/* Temporary define */
15+
#define idtentry_enter_user irqentry_enter_from_user_mode
16+
1417
void idtentry_exit_user(struct pt_regs *regs);
1518

1619
typedef struct idtentry_state {

arch/x86/include/asm/thread_info.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,6 @@ struct thread_info {
133133
#define _TIF_X32 (1 << TIF_X32)
134134
#define _TIF_FSCHECK (1 << TIF_FSCHECK)
135135

136-
/* Work to do before invoking the actual syscall. */
137-
#define _TIF_WORK_SYSCALL_ENTRY \
138-
(_TIF_SYSCALL_TRACE | _TIF_SYSCALL_EMU | _TIF_SYSCALL_AUDIT | \
139-
_TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT)
140-
141136
/* flags to check in __switch_to() */
142137
#define _TIF_WORK_CTXSW_BASE \
143138
(_TIF_NOCPUID | _TIF_NOTSC | _TIF_BLOCKSTEP | \

0 commit comments

Comments
 (0)