x86/entry: Assert that syscalls are on the right stack
[platform/kernel/linux-starfive.git] / arch / x86 / entry / common.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * common.c - C code for kernel entry and exit
4  * Copyright (c) 2015 Andrew Lutomirski
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/sched/task_stack.h>
13 #include <linux/mm.h>
14 #include <linux/smp.h>
15 #include <linux/errno.h>
16 #include <linux/ptrace.h>
17 #include <linux/tracehook.h>
18 #include <linux/audit.h>
19 #include <linux/seccomp.h>
20 #include <linux/signal.h>
21 #include <linux/export.h>
22 #include <linux/context_tracking.h>
23 #include <linux/user-return-notifier.h>
24 #include <linux/nospec.h>
25 #include <linux/uprobes.h>
26 #include <linux/livepatch.h>
27 #include <linux/syscalls.h>
28 #include <linux/uaccess.h>
29
30 #ifdef CONFIG_XEN_PV
31 #include <xen/xen-ops.h>
32 #include <xen/events.h>
33 #endif
34
35 #include <asm/desc.h>
36 #include <asm/traps.h>
37 #include <asm/vdso.h>
38 #include <asm/cpufeature.h>
39 #include <asm/fpu/api.h>
40 #include <asm/nospec-branch.h>
41 #include <asm/io_bitmap.h>
42 #include <asm/syscall.h>
43 #include <asm/irq_stack.h>
44
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/syscalls.h>
47
48 /* Check that the stack and regs on entry from user mode are sane. */
49 static void check_user_regs(struct pt_regs *regs)
50 {
51         if (IS_ENABLED(CONFIG_DEBUG_ENTRY)) {
52                 WARN_ON_ONCE(!on_thread_stack());
53                 WARN_ON_ONCE(regs != task_pt_regs(current));
54         }
55 }
56
57 #ifdef CONFIG_CONTEXT_TRACKING
58 /**
59  * enter_from_user_mode - Establish state when coming from user mode
60  *
61  * Syscall entry disables interrupts, but user mode is traced as interrupts
62  * enabled. Also with NO_HZ_FULL RCU might be idle.
63  *
64  * 1) Tell lockdep that interrupts are disabled
65  * 2) Invoke context tracking if enabled to reactivate RCU
66  * 3) Trace interrupts off state
67  */
68 static noinstr void enter_from_user_mode(void)
69 {
70         enum ctx_state state = ct_state();
71
72         lockdep_hardirqs_off(CALLER_ADDR0);
73         user_exit_irqoff();
74
75         instrumentation_begin();
76         CT_WARN_ON(state != CONTEXT_USER);
77         trace_hardirqs_off_finish();
78         instrumentation_end();
79 }
80 #else
81 static __always_inline void enter_from_user_mode(void)
82 {
83         lockdep_hardirqs_off(CALLER_ADDR0);
84         instrumentation_begin();
85         trace_hardirqs_off_finish();
86         instrumentation_end();
87 }
88 #endif
89
90 /**
91  * exit_to_user_mode - Fixup state when exiting to user mode
92  *
93  * Syscall exit enables interrupts, but the kernel state is interrupts
94  * disabled when this is invoked. Also tell RCU about it.
95  *
96  * 1) Trace interrupts on state
97  * 2) Invoke context tracking if enabled to adjust RCU state
98  * 3) Clear CPU buffers if CPU is affected by MDS and the migitation is on.
99  * 4) Tell lockdep that interrupts are enabled
100  */
101 static __always_inline void exit_to_user_mode(void)
102 {
103         instrumentation_begin();
104         trace_hardirqs_on_prepare();
105         lockdep_hardirqs_on_prepare(CALLER_ADDR0);
106         instrumentation_end();
107
108         user_enter_irqoff();
109         mds_user_clear_cpu_buffers();
110         lockdep_hardirqs_on(CALLER_ADDR0);
111 }
112
113 static void do_audit_syscall_entry(struct pt_regs *regs, u32 arch)
114 {
115 #ifdef CONFIG_X86_64
116         if (arch == AUDIT_ARCH_X86_64) {
117                 audit_syscall_entry(regs->orig_ax, regs->di,
118                                     regs->si, regs->dx, regs->r10);
119         } else
120 #endif
121         {
122                 audit_syscall_entry(regs->orig_ax, regs->bx,
123                                     regs->cx, regs->dx, regs->si);
124         }
125 }
126
127 /*
128  * Returns the syscall nr to run (which should match regs->orig_ax) or -1
129  * to skip the syscall.
130  */
131 static long syscall_trace_enter(struct pt_regs *regs)
132 {
133         u32 arch = in_ia32_syscall() ? AUDIT_ARCH_I386 : AUDIT_ARCH_X86_64;
134
135         struct thread_info *ti = current_thread_info();
136         unsigned long ret = 0;
137         u32 work;
138
139         work = READ_ONCE(ti->flags);
140
141         if (work & (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_EMU)) {
142                 ret = tracehook_report_syscall_entry(regs);
143                 if (ret || (work & _TIF_SYSCALL_EMU))
144                         return -1L;
145         }
146
147 #ifdef CONFIG_SECCOMP
148         /*
149          * Do seccomp after ptrace, to catch any tracer changes.
150          */
151         if (work & _TIF_SECCOMP) {
152                 struct seccomp_data sd;
153
154                 sd.arch = arch;
155                 sd.nr = regs->orig_ax;
156                 sd.instruction_pointer = regs->ip;
157 #ifdef CONFIG_X86_64
158                 if (arch == AUDIT_ARCH_X86_64) {
159                         sd.args[0] = regs->di;
160                         sd.args[1] = regs->si;
161                         sd.args[2] = regs->dx;
162                         sd.args[3] = regs->r10;
163                         sd.args[4] = regs->r8;
164                         sd.args[5] = regs->r9;
165                 } else
166 #endif
167                 {
168                         sd.args[0] = regs->bx;
169                         sd.args[1] = regs->cx;
170                         sd.args[2] = regs->dx;
171                         sd.args[3] = regs->si;
172                         sd.args[4] = regs->di;
173                         sd.args[5] = regs->bp;
174                 }
175
176                 ret = __secure_computing(&sd);
177                 if (ret == -1)
178                         return ret;
179         }
180 #endif
181
182         if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
183                 trace_sys_enter(regs, regs->orig_ax);
184
185         do_audit_syscall_entry(regs, arch);
186
187         return ret ?: regs->orig_ax;
188 }
189
190 #define EXIT_TO_USERMODE_LOOP_FLAGS                             \
191         (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE |   \
192          _TIF_NEED_RESCHED | _TIF_USER_RETURN_NOTIFY | _TIF_PATCH_PENDING)
193
194 static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
195 {
196         /*
197          * In order to return to user mode, we need to have IRQs off with
198          * none of EXIT_TO_USERMODE_LOOP_FLAGS set.  Several of these flags
199          * can be set at any time on preemptible kernels if we have IRQs on,
200          * so we need to loop.  Disabling preemption wouldn't help: doing the
201          * work to clear some of the flags can sleep.
202          */
203         while (true) {
204                 /* We have work to do. */
205                 local_irq_enable();
206
207                 if (cached_flags & _TIF_NEED_RESCHED)
208                         schedule();
209
210                 if (cached_flags & _TIF_UPROBE)
211                         uprobe_notify_resume(regs);
212
213                 if (cached_flags & _TIF_PATCH_PENDING)
214                         klp_update_patch_state(current);
215
216                 /* deal with pending signal delivery */
217                 if (cached_flags & _TIF_SIGPENDING)
218                         do_signal(regs);
219
220                 if (cached_flags & _TIF_NOTIFY_RESUME) {
221                         clear_thread_flag(TIF_NOTIFY_RESUME);
222                         tracehook_notify_resume(regs);
223                         rseq_handle_notify_resume(NULL, regs);
224                 }
225
226                 if (cached_flags & _TIF_USER_RETURN_NOTIFY)
227                         fire_user_return_notifiers();
228
229                 /* Disable IRQs and retry */
230                 local_irq_disable();
231
232                 cached_flags = READ_ONCE(current_thread_info()->flags);
233
234                 if (!(cached_flags & EXIT_TO_USERMODE_LOOP_FLAGS))
235                         break;
236         }
237 }
238
239 static void __prepare_exit_to_usermode(struct pt_regs *regs)
240 {
241         struct thread_info *ti = current_thread_info();
242         u32 cached_flags;
243
244         addr_limit_user_check();
245
246         lockdep_assert_irqs_disabled();
247         lockdep_sys_exit();
248
249         cached_flags = READ_ONCE(ti->flags);
250
251         if (unlikely(cached_flags & EXIT_TO_USERMODE_LOOP_FLAGS))
252                 exit_to_usermode_loop(regs, cached_flags);
253
254         /* Reload ti->flags; we may have rescheduled above. */
255         cached_flags = READ_ONCE(ti->flags);
256
257         if (unlikely(cached_flags & _TIF_IO_BITMAP))
258                 tss_update_io_bitmap();
259
260         fpregs_assert_state_consistent();
261         if (unlikely(cached_flags & _TIF_NEED_FPU_LOAD))
262                 switch_fpu_return();
263
264 #ifdef CONFIG_COMPAT
265         /*
266          * Compat syscalls set TS_COMPAT.  Make sure we clear it before
267          * returning to user mode.  We need to clear it *after* signal
268          * handling, because syscall restart has a fixup for compat
269          * syscalls.  The fixup is exercised by the ptrace_syscall_32
270          * selftest.
271          *
272          * We also need to clear TS_REGS_POKED_I386: the 32-bit tracer
273          * special case only applies after poking regs and before the
274          * very next return to user mode.
275          */
276         ti->status &= ~(TS_COMPAT|TS_I386_REGS_POKED);
277 #endif
278 }
279
280 __visible noinstr void prepare_exit_to_usermode(struct pt_regs *regs)
281 {
282         instrumentation_begin();
283         __prepare_exit_to_usermode(regs);
284         instrumentation_end();
285         exit_to_user_mode();
286 }
287
288 #define SYSCALL_EXIT_WORK_FLAGS                         \
289         (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT |      \
290          _TIF_SINGLESTEP | _TIF_SYSCALL_TRACEPOINT)
291
292 static void syscall_slow_exit_work(struct pt_regs *regs, u32 cached_flags)
293 {
294         bool step;
295
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 static void __syscall_return_slowpath(struct pt_regs *regs)
315 {
316         struct thread_info *ti = current_thread_info();
317         u32 cached_flags = READ_ONCE(ti->flags);
318
319         CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
320
321         if (IS_ENABLED(CONFIG_PROVE_LOCKING) &&
322             WARN(irqs_disabled(), "syscall %ld left IRQs disabled", regs->orig_ax))
323                 local_irq_enable();
324
325         rseq_syscall(regs);
326
327         /*
328          * First do one-time work.  If these work items are enabled, we
329          * want to run them exactly once per syscall exit with IRQs on.
330          */
331         if (unlikely(cached_flags & SYSCALL_EXIT_WORK_FLAGS))
332                 syscall_slow_exit_work(regs, cached_flags);
333
334         local_irq_disable();
335         __prepare_exit_to_usermode(regs);
336 }
337
338 /*
339  * Called with IRQs on and fully valid regs.  Returns with IRQs off in a
340  * state such that we can immediately switch to user mode.
341  */
342 __visible noinstr void syscall_return_slowpath(struct pt_regs *regs)
343 {
344         instrumentation_begin();
345         __syscall_return_slowpath(regs);
346         instrumentation_end();
347         exit_to_user_mode();
348 }
349
350 #ifdef CONFIG_X86_64
351 __visible noinstr void do_syscall_64(unsigned long nr, struct pt_regs *regs)
352 {
353         struct thread_info *ti;
354
355         check_user_regs(regs);
356
357         enter_from_user_mode();
358         instrumentation_begin();
359
360         local_irq_enable();
361         ti = current_thread_info();
362         if (READ_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY)
363                 nr = syscall_trace_enter(regs);
364
365         if (likely(nr < NR_syscalls)) {
366                 nr = array_index_nospec(nr, NR_syscalls);
367                 regs->ax = sys_call_table[nr](regs);
368 #ifdef CONFIG_X86_X32_ABI
369         } else if (likely((nr & __X32_SYSCALL_BIT) &&
370                           (nr & ~__X32_SYSCALL_BIT) < X32_NR_syscalls)) {
371                 nr = array_index_nospec(nr & ~__X32_SYSCALL_BIT,
372                                         X32_NR_syscalls);
373                 regs->ax = x32_sys_call_table[nr](regs);
374 #endif
375         }
376         __syscall_return_slowpath(regs);
377
378         instrumentation_end();
379         exit_to_user_mode();
380 }
381 #endif
382
383 #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
384 /*
385  * Does a 32-bit syscall.  Called with IRQs on in CONTEXT_KERNEL.  Does
386  * all entry and exit work and returns with IRQs off.  This function is
387  * extremely hot in workloads that use it, and it's usually called from
388  * do_fast_syscall_32, so forcibly inline it to improve performance.
389  */
390 static void do_syscall_32_irqs_on(struct pt_regs *regs)
391 {
392         struct thread_info *ti = current_thread_info();
393         unsigned int nr = (unsigned int)regs->orig_ax;
394
395 #ifdef CONFIG_IA32_EMULATION
396         ti->status |= TS_COMPAT;
397 #endif
398
399         if (READ_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY) {
400                 /*
401                  * Subtlety here: if ptrace pokes something larger than
402                  * 2^32-1 into orig_ax, this truncates it.  This may or
403                  * may not be necessary, but it matches the old asm
404                  * behavior.
405                  */
406                 nr = syscall_trace_enter(regs);
407         }
408
409         if (likely(nr < IA32_NR_syscalls)) {
410                 nr = array_index_nospec(nr, IA32_NR_syscalls);
411                 regs->ax = ia32_sys_call_table[nr](regs);
412         }
413
414         __syscall_return_slowpath(regs);
415 }
416
417 /* Handles int $0x80 */
418 __visible noinstr void do_int80_syscall_32(struct pt_regs *regs)
419 {
420         check_user_regs(regs);
421
422         enter_from_user_mode();
423         instrumentation_begin();
424
425         local_irq_enable();
426         do_syscall_32_irqs_on(regs);
427
428         instrumentation_end();
429         exit_to_user_mode();
430 }
431
432 static bool __do_fast_syscall_32(struct pt_regs *regs)
433 {
434         int res;
435
436         /* Fetch EBP from where the vDSO stashed it. */
437         if (IS_ENABLED(CONFIG_X86_64)) {
438                 /*
439                  * Micro-optimization: the pointer we're following is
440                  * explicitly 32 bits, so it can't be out of range.
441                  */
442                 res = __get_user(*(u32 *)&regs->bp,
443                          (u32 __user __force *)(unsigned long)(u32)regs->sp);
444         } else {
445                 res = get_user(*(u32 *)&regs->bp,
446                        (u32 __user __force *)(unsigned long)(u32)regs->sp);
447         }
448
449         if (res) {
450                 /* User code screwed up. */
451                 regs->ax = -EFAULT;
452                 local_irq_disable();
453                 __prepare_exit_to_usermode(regs);
454                 return false;
455         }
456
457         /* Now this is just like a normal syscall. */
458         do_syscall_32_irqs_on(regs);
459         return true;
460 }
461
462 /* Returns 0 to return using IRET or 1 to return using SYSEXIT/SYSRETL. */
463 __visible noinstr long do_fast_syscall_32(struct pt_regs *regs)
464 {
465         /*
466          * Called using the internal vDSO SYSENTER/SYSCALL32 calling
467          * convention.  Adjust regs so it looks like we entered using int80.
468          */
469         unsigned long landing_pad = (unsigned long)current->mm->context.vdso +
470                                         vdso_image_32.sym_int80_landing_pad;
471         bool success;
472
473         check_user_regs(regs);
474
475         /*
476          * SYSENTER loses EIP, and even SYSCALL32 needs us to skip forward
477          * so that 'regs->ip -= 2' lands back on an int $0x80 instruction.
478          * Fix it up.
479          */
480         regs->ip = landing_pad;
481
482         enter_from_user_mode();
483         instrumentation_begin();
484
485         local_irq_enable();
486         success = __do_fast_syscall_32(regs);
487
488         instrumentation_end();
489         exit_to_user_mode();
490
491         /* If it failed, keep it simple: use IRET. */
492         if (!success)
493                 return 0;
494
495 #ifdef CONFIG_X86_64
496         /*
497          * Opportunistic SYSRETL: if possible, try to return using SYSRETL.
498          * SYSRETL is available on all 64-bit CPUs, so we don't need to
499          * bother with SYSEXIT.
500          *
501          * Unlike 64-bit opportunistic SYSRET, we can't check that CX == IP,
502          * because the ECX fixup above will ensure that this is essentially
503          * never the case.
504          */
505         return regs->cs == __USER32_CS && regs->ss == __USER_DS &&
506                 regs->ip == landing_pad &&
507                 (regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF)) == 0;
508 #else
509         /*
510          * Opportunistic SYSEXIT: if possible, try to return using SYSEXIT.
511          *
512          * Unlike 64-bit opportunistic SYSRET, we can't check that CX == IP,
513          * because the ECX fixup above will ensure that this is essentially
514          * never the case.
515          *
516          * We don't allow syscalls at all from VM86 mode, but we still
517          * need to check VM, because we might be returning from sys_vm86.
518          */
519         return static_cpu_has(X86_FEATURE_SEP) &&
520                 regs->cs == __USER_CS && regs->ss == __USER_DS &&
521                 regs->ip == landing_pad &&
522                 (regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF | X86_EFLAGS_VM)) == 0;
523 #endif
524 }
525 #endif
526
527 SYSCALL_DEFINE0(ni_syscall)
528 {
529         return -ENOSYS;
530 }
531
532 /**
533  * idtentry_enter_cond_rcu - Handle state tracking on idtentry with conditional
534  *                           RCU handling
535  * @regs:       Pointer to pt_regs of interrupted context
536  *
537  * Invokes:
538  *  - lockdep irqflag state tracking as low level ASM entry disabled
539  *    interrupts.
540  *
541  *  - Context tracking if the exception hit user mode.
542  *
543  *  - The hardirq tracer to keep the state consistent as low level ASM
544  *    entry disabled interrupts.
545  *
546  * For kernel mode entries RCU handling is done conditional. If RCU is
547  * watching then the only RCU requirement is to check whether the tick has
548  * to be restarted. If RCU is not watching then rcu_irq_enter() has to be
549  * invoked on entry and rcu_irq_exit() on exit.
550  *
551  * Avoiding the rcu_irq_enter/exit() calls is an optimization but also
552  * solves the problem of kernel mode pagefaults which can schedule, which
553  * is not possible after invoking rcu_irq_enter() without undoing it.
554  *
555  * For user mode entries enter_from_user_mode() must be invoked to
556  * establish the proper context for NOHZ_FULL. Otherwise scheduling on exit
557  * would not be possible.
558  *
559  * Returns: True if RCU has been adjusted on a kernel entry
560  *          False otherwise
561  *
562  * The return value must be fed into the rcu_exit argument of
563  * idtentry_exit_cond_rcu().
564  */
565 bool noinstr idtentry_enter_cond_rcu(struct pt_regs *regs)
566 {
567         if (user_mode(regs)) {
568                 enter_from_user_mode();
569                 return false;
570         }
571
572         /*
573          * If this entry hit the idle task invoke rcu_irq_enter() whether
574          * RCU is watching or not.
575          *
576          * Interupts can nest when the first interrupt invokes softirq
577          * processing on return which enables interrupts.
578          *
579          * Scheduler ticks in the idle task can mark quiescent state and
580          * terminate a grace period, if and only if the timer interrupt is
581          * not nested into another interrupt.
582          *
583          * Checking for __rcu_is_watching() here would prevent the nesting
584          * interrupt to invoke rcu_irq_enter(). If that nested interrupt is
585          * the tick then rcu_flavor_sched_clock_irq() would wrongfully
586          * assume that it is the first interupt and eventually claim
587          * quiescient state and end grace periods prematurely.
588          *
589          * Unconditionally invoke rcu_irq_enter() so RCU state stays
590          * consistent.
591          *
592          * TINY_RCU does not support EQS, so let the compiler eliminate
593          * this part when enabled.
594          */
595         if (!IS_ENABLED(CONFIG_TINY_RCU) && is_idle_task(current)) {
596                 /*
597                  * If RCU is not watching then the same careful
598                  * sequence vs. lockdep and tracing is required
599                  * as in enter_from_user_mode().
600                  */
601                 lockdep_hardirqs_off(CALLER_ADDR0);
602                 rcu_irq_enter();
603                 instrumentation_begin();
604                 trace_hardirqs_off_finish();
605                 instrumentation_end();
606
607                 return true;
608         }
609
610         /*
611          * If RCU is watching then RCU only wants to check whether it needs
612          * to restart the tick in NOHZ mode. rcu_irq_enter_check_tick()
613          * already contains a warning when RCU is not watching, so no point
614          * in having another one here.
615          */
616         instrumentation_begin();
617         rcu_irq_enter_check_tick();
618         /* Use the combo lockdep/tracing function */
619         trace_hardirqs_off();
620         instrumentation_end();
621
622         return false;
623 }
624
625 static void idtentry_exit_cond_resched(struct pt_regs *regs, bool may_sched)
626 {
627         if (may_sched && !preempt_count()) {
628                 /* Sanity check RCU and thread stack */
629                 rcu_irq_exit_check_preempt();
630                 if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
631                         WARN_ON_ONCE(!on_thread_stack());
632                 if (need_resched())
633                         preempt_schedule_irq();
634         }
635         /* Covers both tracing and lockdep */
636         trace_hardirqs_on();
637 }
638
639 /**
640  * idtentry_exit_cond_rcu - Handle return from exception with conditional RCU
641  *                          handling
642  * @regs:       Pointer to pt_regs (exception entry regs)
643  * @rcu_exit:   Invoke rcu_irq_exit() if true
644  *
645  * Depending on the return target (kernel/user) this runs the necessary
646  * preemption and work checks if possible and reguired and returns to
647  * the caller with interrupts disabled and no further work pending.
648  *
649  * This is the last action before returning to the low level ASM code which
650  * just needs to return to the appropriate context.
651  *
652  * Counterpart to idtentry_enter_cond_rcu(). The return value of the entry
653  * function must be fed into the @rcu_exit argument.
654  */
655 void noinstr idtentry_exit_cond_rcu(struct pt_regs *regs, bool rcu_exit)
656 {
657         lockdep_assert_irqs_disabled();
658
659         /* Check whether this returns to user mode */
660         if (user_mode(regs)) {
661                 prepare_exit_to_usermode(regs);
662         } else if (regs->flags & X86_EFLAGS_IF) {
663                 /*
664                  * If RCU was not watching on entry this needs to be done
665                  * carefully and needs the same ordering of lockdep/tracing
666                  * and RCU as the return to user mode path.
667                  */
668                 if (rcu_exit) {
669                         instrumentation_begin();
670                         /* Tell the tracer that IRET will enable interrupts */
671                         trace_hardirqs_on_prepare();
672                         lockdep_hardirqs_on_prepare(CALLER_ADDR0);
673                         instrumentation_end();
674                         rcu_irq_exit();
675                         lockdep_hardirqs_on(CALLER_ADDR0);
676                         return;
677                 }
678
679                 instrumentation_begin();
680                 idtentry_exit_cond_resched(regs, IS_ENABLED(CONFIG_PREEMPTION));
681                 instrumentation_end();
682         } else {
683                 /*
684                  * IRQ flags state is correct already. Just tell RCU if it
685                  * was not watching on entry.
686                  */
687                 if (rcu_exit)
688                         rcu_irq_exit();
689         }
690 }
691
692 /**
693  * idtentry_enter_user - Handle state tracking on idtentry from user mode
694  * @regs:       Pointer to pt_regs of interrupted context
695  *
696  * Invokes enter_from_user_mode() to establish the proper context for
697  * NOHZ_FULL. Otherwise scheduling on exit would not be possible.
698  */
699 void noinstr idtentry_enter_user(struct pt_regs *regs)
700 {
701         enter_from_user_mode();
702 }
703
704 /**
705  * idtentry_exit_user - Handle return from exception to user mode
706  * @regs:       Pointer to pt_regs (exception entry regs)
707  *
708  * Runs the necessary preemption and work checks and returns to the caller
709  * with interrupts disabled and no further work pending.
710  *
711  * This is the last action before returning to the low level ASM code which
712  * just needs to return to the appropriate context.
713  *
714  * Counterpart to idtentry_enter_user().
715  */
716 void noinstr idtentry_exit_user(struct pt_regs *regs)
717 {
718         lockdep_assert_irqs_disabled();
719
720         prepare_exit_to_usermode(regs);
721 }
722
723 #ifdef CONFIG_XEN_PV
724 #ifndef CONFIG_PREEMPTION
725 /*
726  * Some hypercalls issued by the toolstack can take many 10s of
727  * seconds. Allow tasks running hypercalls via the privcmd driver to
728  * be voluntarily preempted even if full kernel preemption is
729  * disabled.
730  *
731  * Such preemptible hypercalls are bracketed by
732  * xen_preemptible_hcall_begin() and xen_preemptible_hcall_end()
733  * calls.
734  */
735 DEFINE_PER_CPU(bool, xen_in_preemptible_hcall);
736 EXPORT_SYMBOL_GPL(xen_in_preemptible_hcall);
737
738 /*
739  * In case of scheduling the flag must be cleared and restored after
740  * returning from schedule as the task might move to a different CPU.
741  */
742 static __always_inline bool get_and_clear_inhcall(void)
743 {
744         bool inhcall = __this_cpu_read(xen_in_preemptible_hcall);
745
746         __this_cpu_write(xen_in_preemptible_hcall, false);
747         return inhcall;
748 }
749
750 static __always_inline void restore_inhcall(bool inhcall)
751 {
752         __this_cpu_write(xen_in_preemptible_hcall, inhcall);
753 }
754 #else
755 static __always_inline bool get_and_clear_inhcall(void) { return false; }
756 static __always_inline void restore_inhcall(bool inhcall) { }
757 #endif
758
759 static void __xen_pv_evtchn_do_upcall(void)
760 {
761         irq_enter_rcu();
762         inc_irq_stat(irq_hv_callback_count);
763
764         xen_hvm_evtchn_do_upcall();
765
766         irq_exit_rcu();
767 }
768
769 __visible noinstr void xen_pv_evtchn_do_upcall(struct pt_regs *regs)
770 {
771         struct pt_regs *old_regs;
772         bool inhcall, rcu_exit;
773
774         rcu_exit = idtentry_enter_cond_rcu(regs);
775         old_regs = set_irq_regs(regs);
776
777         instrumentation_begin();
778         run_on_irqstack_cond(__xen_pv_evtchn_do_upcall, NULL, regs);
779         instrumentation_begin();
780
781         set_irq_regs(old_regs);
782
783         inhcall = get_and_clear_inhcall();
784         if (inhcall && !WARN_ON_ONCE(rcu_exit)) {
785                 instrumentation_begin();
786                 idtentry_exit_cond_resched(regs, true);
787                 instrumentation_end();
788                 restore_inhcall(inhcall);
789         } else {
790                 idtentry_exit_cond_rcu(regs, rcu_exit);
791         }
792 }
793 #endif /* CONFIG_XEN_PV */