x86/entry: Hide two syscall entry assertions behind CONFIG_DEBUG_ENTRY
[deliverable/linux.git] / arch / x86 / entry / common.c
CommitLineData
1f484aa6
AL
1/*
2 * common.c - C code for kernel entry and exit
3 * Copyright (c) 2015 Andrew Lutomirski
4 * GPL v2
5 *
6 * Based on asm and ptrace code by many authors. The code here originated
7 * in ptrace.c and signal.c.
8 */
9
10#include <linux/kernel.h>
11#include <linux/sched.h>
12#include <linux/mm.h>
13#include <linux/smp.h>
14#include <linux/errno.h>
15#include <linux/ptrace.h>
16#include <linux/tracehook.h>
17#include <linux/audit.h>
18#include <linux/seccomp.h>
19#include <linux/signal.h>
20#include <linux/export.h>
21#include <linux/context_tracking.h>
22#include <linux/user-return-notifier.h>
23#include <linux/uprobes.h>
24
25#include <asm/desc.h>
26#include <asm/traps.h>
710246df
AL
27#include <asm/vdso.h>
28#include <asm/uaccess.h>
1f484aa6
AL
29
30#define CREATE_TRACE_POINTS
31#include <trace/events/syscalls.h>
32
feed36cd
AL
33#ifdef CONFIG_CONTEXT_TRACKING
34/* Called on entry from user mode with IRQs off. */
35__visible void enter_from_user_mode(void)
36{
37 CT_WARN_ON(ct_state() != CONTEXT_USER);
38 user_exit();
39}
40#endif
41
1f484aa6
AL
42static void do_audit_syscall_entry(struct pt_regs *regs, u32 arch)
43{
44#ifdef CONFIG_X86_64
45 if (arch == AUDIT_ARCH_X86_64) {
46 audit_syscall_entry(regs->orig_ax, regs->di,
47 regs->si, regs->dx, regs->r10);
48 } else
49#endif
50 {
51 audit_syscall_entry(regs->orig_ax, regs->bx,
52 regs->cx, regs->dx, regs->si);
53 }
54}
55
56/*
57 * We can return 0 to resume the syscall or anything else to go to phase
58 * 2. If we resume the syscall, we need to put something appropriate in
59 * regs->orig_ax.
60 *
61 * NB: We don't have full pt_regs here, but regs->orig_ax and regs->ax
62 * are fully functional.
63 *
64 * For phase 2's benefit, our return value is:
65 * 0: resume the syscall
66 * 1: go to phase 2; no seccomp phase 2 needed
67 * anything else: go to phase 2; pass return value to seccomp
68 */
69unsigned long syscall_trace_enter_phase1(struct pt_regs *regs, u32 arch)
70{
71 unsigned long ret = 0;
72 u32 work;
73
4aabd140
AL
74 if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
75 BUG_ON(regs != task_pt_regs(current));
1f484aa6
AL
76
77 work = ACCESS_ONCE(current_thread_info()->flags) &
78 _TIF_WORK_SYSCALL_ENTRY;
79
feed36cd 80#ifdef CONFIG_CONTEXT_TRACKING
1f484aa6
AL
81 /*
82 * If TIF_NOHZ is set, we are required to call user_exit() before
83 * doing anything that could touch RCU.
84 */
85 if (work & _TIF_NOHZ) {
feed36cd 86 enter_from_user_mode();
1f484aa6
AL
87 work &= ~_TIF_NOHZ;
88 }
feed36cd 89#endif
1f484aa6
AL
90
91#ifdef CONFIG_SECCOMP
92 /*
93 * Do seccomp first -- it should minimize exposure of other
94 * code, and keeping seccomp fast is probably more valuable
95 * than the rest of this.
96 */
97 if (work & _TIF_SECCOMP) {
98 struct seccomp_data sd;
99
100 sd.arch = arch;
101 sd.nr = regs->orig_ax;
102 sd.instruction_pointer = regs->ip;
103#ifdef CONFIG_X86_64
104 if (arch == AUDIT_ARCH_X86_64) {
105 sd.args[0] = regs->di;
106 sd.args[1] = regs->si;
107 sd.args[2] = regs->dx;
108 sd.args[3] = regs->r10;
109 sd.args[4] = regs->r8;
110 sd.args[5] = regs->r9;
111 } else
112#endif
113 {
114 sd.args[0] = regs->bx;
115 sd.args[1] = regs->cx;
116 sd.args[2] = regs->dx;
117 sd.args[3] = regs->si;
118 sd.args[4] = regs->di;
119 sd.args[5] = regs->bp;
120 }
121
122 BUILD_BUG_ON(SECCOMP_PHASE1_OK != 0);
123 BUILD_BUG_ON(SECCOMP_PHASE1_SKIP != 1);
124
125 ret = seccomp_phase1(&sd);
126 if (ret == SECCOMP_PHASE1_SKIP) {
127 regs->orig_ax = -1;
128 ret = 0;
129 } else if (ret != SECCOMP_PHASE1_OK) {
130 return ret; /* Go directly to phase 2 */
131 }
132
133 work &= ~_TIF_SECCOMP;
134 }
135#endif
136
137 /* Do our best to finish without phase 2. */
138 if (work == 0)
139 return ret; /* seccomp and/or nohz only (ret == 0 here) */
140
141#ifdef CONFIG_AUDITSYSCALL
142 if (work == _TIF_SYSCALL_AUDIT) {
143 /*
144 * If there is no more work to be done except auditing,
145 * then audit in phase 1. Phase 2 always audits, so, if
146 * we audit here, then we can't go on to phase 2.
147 */
148 do_audit_syscall_entry(regs, arch);
149 return 0;
150 }
151#endif
152
153 return 1; /* Something is enabled that we can't handle in phase 1 */
154}
155
156/* Returns the syscall nr to run (which should match regs->orig_ax). */
157long syscall_trace_enter_phase2(struct pt_regs *regs, u32 arch,
158 unsigned long phase1_result)
159{
160 long ret = 0;
161 u32 work = ACCESS_ONCE(current_thread_info()->flags) &
162 _TIF_WORK_SYSCALL_ENTRY;
163
4aabd140
AL
164 if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
165 BUG_ON(regs != task_pt_regs(current));
1f484aa6
AL
166
167 /*
168 * If we stepped into a sysenter/syscall insn, it trapped in
169 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
170 * If user-mode had set TF itself, then it's still clear from
171 * do_debug() and we need to set it again to restore the user
172 * state. If we entered on the slow path, TF was already set.
173 */
174 if (work & _TIF_SINGLESTEP)
175 regs->flags |= X86_EFLAGS_TF;
176
177#ifdef CONFIG_SECCOMP
178 /*
179 * Call seccomp_phase2 before running the other hooks so that
180 * they can see any changes made by a seccomp tracer.
181 */
182 if (phase1_result > 1 && seccomp_phase2(phase1_result)) {
183 /* seccomp failures shouldn't expose any additional code. */
184 return -1;
185 }
186#endif
187
188 if (unlikely(work & _TIF_SYSCALL_EMU))
189 ret = -1L;
190
191 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
192 tracehook_report_syscall_entry(regs))
193 ret = -1L;
194
195 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
196 trace_sys_enter(regs, regs->orig_ax);
197
198 do_audit_syscall_entry(regs, arch);
199
200 return ret ?: regs->orig_ax;
201}
202
203long syscall_trace_enter(struct pt_regs *regs)
204{
205 u32 arch = is_ia32_task() ? AUDIT_ARCH_I386 : AUDIT_ARCH_X86_64;
206 unsigned long phase1_result = syscall_trace_enter_phase1(regs, arch);
207
208 if (phase1_result == 0)
209 return regs->orig_ax;
210 else
211 return syscall_trace_enter_phase2(regs, arch, phase1_result);
212}
213
c5c46f59
AL
214static struct thread_info *pt_regs_to_thread_info(struct pt_regs *regs)
215{
216 unsigned long top_of_stack =
217 (unsigned long)(regs + 1) + TOP_OF_KERNEL_STACK_PADDING;
218 return (struct thread_info *)(top_of_stack - THREAD_SIZE);
219}
220
221/* Called with IRQs disabled. */
222__visible void prepare_exit_to_usermode(struct pt_regs *regs)
223{
460d1245 224 if (IS_ENABLED(CONFIG_PROVE_LOCKING) && WARN_ON(!irqs_disabled()))
c5c46f59
AL
225 local_irq_disable();
226
72f92478
AL
227 lockdep_sys_exit();
228
c5c46f59
AL
229 /*
230 * In order to return to user mode, we need to have IRQs off with
231 * none of _TIF_SIGPENDING, _TIF_NOTIFY_RESUME, _TIF_USER_RETURN_NOTIFY,
232 * _TIF_UPROBE, or _TIF_NEED_RESCHED set. Several of these flags
233 * can be set at any time on preemptable kernels if we have IRQs on,
234 * so we need to loop. Disabling preemption wouldn't help: doing the
235 * work to clear some of the flags can sleep.
236 */
237 while (true) {
238 u32 cached_flags =
239 READ_ONCE(pt_regs_to_thread_info(regs)->flags);
240
241 if (!(cached_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME |
d132803e
AL
242 _TIF_UPROBE | _TIF_NEED_RESCHED |
243 _TIF_USER_RETURN_NOTIFY)))
c5c46f59
AL
244 break;
245
246 /* We have work to do. */
247 local_irq_enable();
248
249 if (cached_flags & _TIF_NEED_RESCHED)
250 schedule();
251
252 if (cached_flags & _TIF_UPROBE)
253 uprobe_notify_resume(regs);
254
255 /* deal with pending signal delivery */
256 if (cached_flags & _TIF_SIGPENDING)
257 do_signal(regs);
258
259 if (cached_flags & _TIF_NOTIFY_RESUME) {
260 clear_thread_flag(TIF_NOTIFY_RESUME);
261 tracehook_notify_resume(regs);
262 }
263
264 if (cached_flags & _TIF_USER_RETURN_NOTIFY)
265 fire_user_return_notifiers();
266
267 /* Disable IRQs and retry */
268 local_irq_disable();
269 }
270
271 user_enter();
272}
273
274/*
275 * Called with IRQs on and fully valid regs. Returns with IRQs off in a
276 * state such that we can immediately switch to user mode.
277 */
278__visible void syscall_return_slowpath(struct pt_regs *regs)
279{
280 struct thread_info *ti = pt_regs_to_thread_info(regs);
281 u32 cached_flags = READ_ONCE(ti->flags);
282 bool step;
283
284 CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
285
460d1245
AL
286 if (IS_ENABLED(CONFIG_PROVE_LOCKING) &&
287 WARN(irqs_disabled(), "syscall %ld left IRQs disabled", regs->orig_ax))
c5c46f59
AL
288 local_irq_enable();
289
290 /*
291 * First do one-time work. If these work items are enabled, we
292 * want to run them exactly once per syscall exit with IRQs on.
293 */
294 if (cached_flags & (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT |
295 _TIF_SINGLESTEP | _TIF_SYSCALL_TRACEPOINT)) {
296 audit_syscall_exit(regs);
297
298 if (cached_flags & _TIF_SYSCALL_TRACEPOINT)
299 trace_sys_exit(regs, regs->ax);
300
301 /*
302 * If TIF_SYSCALL_EMU is set, we only get here because of
303 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
304 * We already reported this syscall instruction in
305 * syscall_trace_enter().
306 */
307 step = unlikely(
308 (cached_flags & (_TIF_SINGLESTEP | _TIF_SYSCALL_EMU))
309 == _TIF_SINGLESTEP);
310 if (step || cached_flags & _TIF_SYSCALL_TRACE)
311 tracehook_report_syscall_exit(regs, step);
312 }
313
314#ifdef CONFIG_COMPAT
315 /*
316 * Compat syscalls set TS_COMPAT. Make sure we clear it before
317 * returning to user mode.
318 */
319 ti->status &= ~TS_COMPAT;
320#endif
321
322 local_irq_disable();
323 prepare_exit_to_usermode(regs);
324}
bd2d3a3b
AL
325
326#if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
327/*
8b13c255 328 * Does a 32-bit syscall. Called with IRQs on and does all entry and
33c52129
AL
329 * exit work and returns with IRQs off. This function is extremely hot
330 * in workloads that use it, and it's usually called from
331 * do_fast_syscall_32, so forcibly inline it to improve performance.
bd2d3a3b 332 */
33c52129 333static __always_inline void do_syscall_32_irqs_on(struct pt_regs *regs)
bd2d3a3b
AL
334{
335 struct thread_info *ti = pt_regs_to_thread_info(regs);
336 unsigned int nr = (unsigned int)regs->orig_ax;
337
338#ifdef CONFIG_IA32_EMULATION
339 ti->status |= TS_COMPAT;
340#endif
341
bd2d3a3b
AL
342 if (READ_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY) {
343 /*
344 * Subtlety here: if ptrace pokes something larger than
345 * 2^32-1 into orig_ax, this truncates it. This may or
346 * may not be necessary, but it matches the old asm
347 * behavior.
348 */
349 nr = syscall_trace_enter(regs);
350 }
351
33c52129 352 if (likely(nr < IA32_NR_syscalls)) {
bd2d3a3b
AL
353 /*
354 * It's possible that a 32-bit syscall implementation
355 * takes a 64-bit parameter but nonetheless assumes that
356 * the high bits are zero. Make sure we zero-extend all
357 * of the args.
358 */
359 regs->ax = ia32_sys_call_table[nr](
360 (unsigned int)regs->bx, (unsigned int)regs->cx,
361 (unsigned int)regs->dx, (unsigned int)regs->si,
362 (unsigned int)regs->di, (unsigned int)regs->bp);
363 }
364
365 syscall_return_slowpath(regs);
366}
710246df 367
8b13c255
AL
368/* Handles int $0x80 */
369__visible void do_int80_syscall_32(struct pt_regs *regs)
370{
371 local_irq_enable();
372 do_syscall_32_irqs_on(regs);
373}
374
5f310f73 375/* Returns 0 to return using IRET or 1 to return using SYSEXIT/SYSRETL. */
7841b408 376__visible long do_fast_syscall_32(struct pt_regs *regs)
710246df
AL
377{
378 /*
379 * Called using the internal vDSO SYSENTER/SYSCALL32 calling
380 * convention. Adjust regs so it looks like we entered using int80.
381 */
382
383 unsigned long landing_pad = (unsigned long)current->mm->context.vdso +
384 vdso_image_32.sym_int80_landing_pad;
385
386 /*
387 * SYSENTER loses EIP, and even SYSCALL32 needs us to skip forward
388 * so that 'regs->ip -= 2' lands back on an int $0x80 instruction.
389 * Fix it up.
390 */
391 regs->ip = landing_pad;
392
393 /*
394 * Fetch ECX from where the vDSO stashed it.
395 *
396 * WARNING: We are in CONTEXT_USER and RCU isn't paying attention!
397 */
398 local_irq_enable();
c68ca678
AL
399 if (
400#ifdef CONFIG_X86_64
401 /*
402 * Micro-optimization: the pointer we're following is explicitly
403 * 32 bits, so it can't be out of range.
404 */
405 __get_user(*(u32 *)&regs->cx,
406 (u32 __user __force *)(unsigned long)(u32)regs->sp)
407#else
408 get_user(*(u32 *)&regs->cx,
409 (u32 __user __force *)(unsigned long)(u32)regs->sp)
410#endif
411 ) {
412
710246df
AL
413 /* User code screwed up. */
414 local_irq_disable();
415 regs->ax = -EFAULT;
416#ifdef CONFIG_CONTEXT_TRACKING
417 enter_from_user_mode();
418#endif
419 prepare_exit_to_usermode(regs);
7841b408 420 return 0; /* Keep it simple: use IRET. */
710246df 421 }
710246df
AL
422
423 /* Now this is just like a normal syscall. */
8b13c255 424 do_syscall_32_irqs_on(regs);
7841b408
AL
425
426#ifdef CONFIG_X86_64
427 /*
428 * Opportunistic SYSRETL: if possible, try to return using SYSRETL.
429 * SYSRETL is available on all 64-bit CPUs, so we don't need to
430 * bother with SYSEXIT.
431 *
432 * Unlike 64-bit opportunistic SYSRET, we can't check that CX == IP,
433 * because the ECX fixup above will ensure that this is essentially
434 * never the case.
435 */
436 return regs->cs == __USER32_CS && regs->ss == __USER_DS &&
437 regs->ip == landing_pad &&
438 (regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF)) == 0;
439#else
5f310f73
AL
440 /*
441 * Opportunistic SYSEXIT: if possible, try to return using SYSEXIT.
442 *
443 * Unlike 64-bit opportunistic SYSRET, we can't check that CX == IP,
444 * because the ECX fixup above will ensure that this is essentially
445 * never the case.
446 *
447 * We don't allow syscalls at all from VM86 mode, but we still
448 * need to check VM, because we might be returning from sys_vm86.
449 */
450 return static_cpu_has(X86_FEATURE_SEP) &&
451 regs->cs == __USER_CS && regs->ss == __USER_DS &&
452 regs->ip == landing_pad &&
453 (regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF | X86_EFLAGS_VM)) == 0;
7841b408 454#endif
710246df 455}
bd2d3a3b 456#endif
This page took 0.049917 seconds and 5 git commands to generate.