at v6.15 19 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef __LINUX_ENTRYCOMMON_H 3#define __LINUX_ENTRYCOMMON_H 4 5#include <linux/static_call_types.h> 6#include <linux/ptrace.h> 7#include <linux/syscalls.h> 8#include <linux/seccomp.h> 9#include <linux/sched.h> 10#include <linux/context_tracking.h> 11#include <linux/livepatch.h> 12#include <linux/resume_user_mode.h> 13#include <linux/tick.h> 14#include <linux/kmsan.h> 15 16#include <asm/entry-common.h> 17 18/* 19 * Define dummy _TIF work flags if not defined by the architecture or for 20 * disabled functionality. 21 */ 22#ifndef _TIF_PATCH_PENDING 23# define _TIF_PATCH_PENDING (0) 24#endif 25 26#ifndef _TIF_UPROBE 27# define _TIF_UPROBE (0) 28#endif 29 30/* 31 * SYSCALL_WORK flags handled in syscall_enter_from_user_mode() 32 */ 33#ifndef ARCH_SYSCALL_WORK_ENTER 34# define ARCH_SYSCALL_WORK_ENTER (0) 35#endif 36 37/* 38 * SYSCALL_WORK flags handled in syscall_exit_to_user_mode() 39 */ 40#ifndef ARCH_SYSCALL_WORK_EXIT 41# define ARCH_SYSCALL_WORK_EXIT (0) 42#endif 43 44#define SYSCALL_WORK_ENTER (SYSCALL_WORK_SECCOMP | \ 45 SYSCALL_WORK_SYSCALL_TRACEPOINT | \ 46 SYSCALL_WORK_SYSCALL_TRACE | \ 47 SYSCALL_WORK_SYSCALL_EMU | \ 48 SYSCALL_WORK_SYSCALL_AUDIT | \ 49 SYSCALL_WORK_SYSCALL_USER_DISPATCH | \ 50 ARCH_SYSCALL_WORK_ENTER) 51#define SYSCALL_WORK_EXIT (SYSCALL_WORK_SYSCALL_TRACEPOINT | \ 52 SYSCALL_WORK_SYSCALL_TRACE | \ 53 SYSCALL_WORK_SYSCALL_AUDIT | \ 54 SYSCALL_WORK_SYSCALL_USER_DISPATCH | \ 55 SYSCALL_WORK_SYSCALL_EXIT_TRAP | \ 56 ARCH_SYSCALL_WORK_EXIT) 57 58/* 59 * TIF flags handled in exit_to_user_mode_loop() 60 */ 61#ifndef ARCH_EXIT_TO_USER_MODE_WORK 62# define ARCH_EXIT_TO_USER_MODE_WORK (0) 63#endif 64 65#define EXIT_TO_USER_MODE_WORK \ 66 (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \ 67 _TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY | \ 68 _TIF_PATCH_PENDING | _TIF_NOTIFY_SIGNAL | \ 69 ARCH_EXIT_TO_USER_MODE_WORK) 70 71/** 72 * arch_enter_from_user_mode - Architecture specific sanity check for user mode regs 73 * @regs: Pointer to currents pt_regs 74 * 75 * Defaults to an empty implementation. Can be replaced by architecture 76 * specific code. 77 * 78 * Invoked from syscall_enter_from_user_mode() in the non-instrumentable 79 * section. Use __always_inline so the compiler cannot push it out of line 80 * and make it instrumentable. 81 */ 82static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs); 83 84#ifndef arch_enter_from_user_mode 85static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs) {} 86#endif 87 88/** 89 * enter_from_user_mode - Establish state when coming from user mode 90 * 91 * Syscall/interrupt entry disables interrupts, but user mode is traced as 92 * interrupts enabled. Also with NO_HZ_FULL RCU might be idle. 93 * 94 * 1) Tell lockdep that interrupts are disabled 95 * 2) Invoke context tracking if enabled to reactivate RCU 96 * 3) Trace interrupts off state 97 * 98 * Invoked from architecture specific syscall entry code with interrupts 99 * disabled. The calling code has to be non-instrumentable. When the 100 * function returns all state is correct and interrupts are still 101 * disabled. The subsequent functions can be instrumented. 102 * 103 * This is invoked when there is architecture specific functionality to be 104 * done between establishing state and enabling interrupts. The caller must 105 * enable interrupts before invoking syscall_enter_from_user_mode_work(). 106 */ 107static __always_inline void enter_from_user_mode(struct pt_regs *regs) 108{ 109 arch_enter_from_user_mode(regs); 110 lockdep_hardirqs_off(CALLER_ADDR0); 111 112 CT_WARN_ON(__ct_state() != CT_STATE_USER); 113 user_exit_irqoff(); 114 115 instrumentation_begin(); 116 kmsan_unpoison_entry_regs(regs); 117 trace_hardirqs_off_finish(); 118 instrumentation_end(); 119} 120 121/** 122 * syscall_enter_from_user_mode_prepare - Establish state and enable interrupts 123 * @regs: Pointer to currents pt_regs 124 * 125 * Invoked from architecture specific syscall entry code with interrupts 126 * disabled. The calling code has to be non-instrumentable. When the 127 * function returns all state is correct, interrupts are enabled and the 128 * subsequent functions can be instrumented. 129 * 130 * This handles lockdep, RCU (context tracking) and tracing state, i.e. 131 * the functionality provided by enter_from_user_mode(). 132 * 133 * This is invoked when there is extra architecture specific functionality 134 * to be done between establishing state and handling user mode entry work. 135 */ 136void syscall_enter_from_user_mode_prepare(struct pt_regs *regs); 137 138long syscall_trace_enter(struct pt_regs *regs, long syscall, 139 unsigned long work); 140 141/** 142 * syscall_enter_from_user_mode_work - Check and handle work before invoking 143 * a syscall 144 * @regs: Pointer to currents pt_regs 145 * @syscall: The syscall number 146 * 147 * Invoked from architecture specific syscall entry code with interrupts 148 * enabled after invoking syscall_enter_from_user_mode_prepare() and extra 149 * architecture specific work. 150 * 151 * Returns: The original or a modified syscall number 152 * 153 * If the returned syscall number is -1 then the syscall should be 154 * skipped. In this case the caller may invoke syscall_set_error() or 155 * syscall_set_return_value() first. If neither of those are called and -1 156 * is returned, then the syscall will fail with ENOSYS. 157 * 158 * It handles the following work items: 159 * 160 * 1) syscall_work flag dependent invocations of 161 * ptrace_report_syscall_entry(), __secure_computing(), trace_sys_enter() 162 * 2) Invocation of audit_syscall_entry() 163 */ 164static __always_inline long syscall_enter_from_user_mode_work(struct pt_regs *regs, long syscall) 165{ 166 unsigned long work = READ_ONCE(current_thread_info()->syscall_work); 167 168 if (work & SYSCALL_WORK_ENTER) 169 syscall = syscall_trace_enter(regs, syscall, work); 170 171 return syscall; 172} 173 174/** 175 * syscall_enter_from_user_mode - Establish state and check and handle work 176 * before invoking a syscall 177 * @regs: Pointer to currents pt_regs 178 * @syscall: The syscall number 179 * 180 * Invoked from architecture specific syscall entry code with interrupts 181 * disabled. The calling code has to be non-instrumentable. When the 182 * function returns all state is correct, interrupts are enabled and the 183 * subsequent functions can be instrumented. 184 * 185 * This is combination of syscall_enter_from_user_mode_prepare() and 186 * syscall_enter_from_user_mode_work(). 187 * 188 * Returns: The original or a modified syscall number. See 189 * syscall_enter_from_user_mode_work() for further explanation. 190 */ 191static __always_inline long syscall_enter_from_user_mode(struct pt_regs *regs, long syscall) 192{ 193 long ret; 194 195 enter_from_user_mode(regs); 196 197 instrumentation_begin(); 198 local_irq_enable(); 199 ret = syscall_enter_from_user_mode_work(regs, syscall); 200 instrumentation_end(); 201 202 return ret; 203} 204 205/** 206 * local_irq_enable_exit_to_user - Exit to user variant of local_irq_enable() 207 * @ti_work: Cached TIF flags gathered with interrupts disabled 208 * 209 * Defaults to local_irq_enable(). Can be supplied by architecture specific 210 * code. 211 */ 212static inline void local_irq_enable_exit_to_user(unsigned long ti_work); 213 214#ifndef local_irq_enable_exit_to_user 215static inline void local_irq_enable_exit_to_user(unsigned long ti_work) 216{ 217 local_irq_enable(); 218} 219#endif 220 221/** 222 * local_irq_disable_exit_to_user - Exit to user variant of local_irq_disable() 223 * 224 * Defaults to local_irq_disable(). Can be supplied by architecture specific 225 * code. 226 */ 227static inline void local_irq_disable_exit_to_user(void); 228 229#ifndef local_irq_disable_exit_to_user 230static inline void local_irq_disable_exit_to_user(void) 231{ 232 local_irq_disable(); 233} 234#endif 235 236/** 237 * arch_exit_to_user_mode_work - Architecture specific TIF work for exit 238 * to user mode. 239 * @regs: Pointer to currents pt_regs 240 * @ti_work: Cached TIF flags gathered with interrupts disabled 241 * 242 * Invoked from exit_to_user_mode_loop() with interrupt enabled 243 * 244 * Defaults to NOOP. Can be supplied by architecture specific code. 245 */ 246static inline void arch_exit_to_user_mode_work(struct pt_regs *regs, 247 unsigned long ti_work); 248 249#ifndef arch_exit_to_user_mode_work 250static inline void arch_exit_to_user_mode_work(struct pt_regs *regs, 251 unsigned long ti_work) 252{ 253} 254#endif 255 256/** 257 * arch_exit_to_user_mode_prepare - Architecture specific preparation for 258 * exit to user mode. 259 * @regs: Pointer to currents pt_regs 260 * @ti_work: Cached TIF flags gathered with interrupts disabled 261 * 262 * Invoked from exit_to_user_mode_prepare() with interrupt disabled as the last 263 * function before return. Defaults to NOOP. 264 */ 265static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs, 266 unsigned long ti_work); 267 268#ifndef arch_exit_to_user_mode_prepare 269static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs, 270 unsigned long ti_work) 271{ 272} 273#endif 274 275/** 276 * arch_exit_to_user_mode - Architecture specific final work before 277 * exit to user mode. 278 * 279 * Invoked from exit_to_user_mode() with interrupt disabled as the last 280 * function before return. Defaults to NOOP. 281 * 282 * This needs to be __always_inline because it is non-instrumentable code 283 * invoked after context tracking switched to user mode. 284 * 285 * An architecture implementation must not do anything complex, no locking 286 * etc. The main purpose is for speculation mitigations. 287 */ 288static __always_inline void arch_exit_to_user_mode(void); 289 290#ifndef arch_exit_to_user_mode 291static __always_inline void arch_exit_to_user_mode(void) { } 292#endif 293 294/** 295 * arch_do_signal_or_restart - Architecture specific signal delivery function 296 * @regs: Pointer to currents pt_regs 297 * 298 * Invoked from exit_to_user_mode_loop(). 299 */ 300void arch_do_signal_or_restart(struct pt_regs *regs); 301 302/** 303 * exit_to_user_mode_loop - do any pending work before leaving to user space 304 */ 305unsigned long exit_to_user_mode_loop(struct pt_regs *regs, 306 unsigned long ti_work); 307 308/** 309 * exit_to_user_mode_prepare - call exit_to_user_mode_loop() if required 310 * @regs: Pointer to pt_regs on entry stack 311 * 312 * 1) check that interrupts are disabled 313 * 2) call tick_nohz_user_enter_prepare() 314 * 3) call exit_to_user_mode_loop() if any flags from 315 * EXIT_TO_USER_MODE_WORK are set 316 * 4) check that interrupts are still disabled 317 */ 318static __always_inline void exit_to_user_mode_prepare(struct pt_regs *regs) 319{ 320 unsigned long ti_work; 321 322 lockdep_assert_irqs_disabled(); 323 324 /* Flush pending rcuog wakeup before the last need_resched() check */ 325 tick_nohz_user_enter_prepare(); 326 327 ti_work = read_thread_flags(); 328 if (unlikely(ti_work & EXIT_TO_USER_MODE_WORK)) 329 ti_work = exit_to_user_mode_loop(regs, ti_work); 330 331 arch_exit_to_user_mode_prepare(regs, ti_work); 332 333 /* Ensure that kernel state is sane for a return to userspace */ 334 kmap_assert_nomap(); 335 lockdep_assert_irqs_disabled(); 336 lockdep_sys_exit(); 337} 338 339/** 340 * exit_to_user_mode - Fixup state when exiting to user mode 341 * 342 * Syscall/interrupt exit enables interrupts, but the kernel state is 343 * interrupts disabled when this is invoked. Also tell RCU about it. 344 * 345 * 1) Trace interrupts on state 346 * 2) Invoke context tracking if enabled to adjust RCU state 347 * 3) Invoke architecture specific last minute exit code, e.g. speculation 348 * mitigations, etc.: arch_exit_to_user_mode() 349 * 4) Tell lockdep that interrupts are enabled 350 * 351 * Invoked from architecture specific code when syscall_exit_to_user_mode() 352 * is not suitable as the last step before returning to userspace. Must be 353 * invoked with interrupts disabled and the caller must be 354 * non-instrumentable. 355 * The caller has to invoke syscall_exit_to_user_mode_work() before this. 356 */ 357static __always_inline void exit_to_user_mode(void) 358{ 359 instrumentation_begin(); 360 trace_hardirqs_on_prepare(); 361 lockdep_hardirqs_on_prepare(); 362 instrumentation_end(); 363 364 user_enter_irqoff(); 365 arch_exit_to_user_mode(); 366 lockdep_hardirqs_on(CALLER_ADDR0); 367} 368 369/** 370 * syscall_exit_to_user_mode_work - Handle work before returning to user mode 371 * @regs: Pointer to currents pt_regs 372 * 373 * Same as step 1 and 2 of syscall_exit_to_user_mode() but without calling 374 * exit_to_user_mode() to perform the final transition to user mode. 375 * 376 * Calling convention is the same as for syscall_exit_to_user_mode() and it 377 * returns with all work handled and interrupts disabled. The caller must 378 * invoke exit_to_user_mode() before actually switching to user mode to 379 * make the final state transitions. Interrupts must stay disabled between 380 * return from this function and the invocation of exit_to_user_mode(). 381 */ 382void syscall_exit_to_user_mode_work(struct pt_regs *regs); 383 384/** 385 * syscall_exit_to_user_mode - Handle work before returning to user mode 386 * @regs: Pointer to currents pt_regs 387 * 388 * Invoked with interrupts enabled and fully valid regs. Returns with all 389 * work handled, interrupts disabled such that the caller can immediately 390 * switch to user mode. Called from architecture specific syscall and ret 391 * from fork code. 392 * 393 * The call order is: 394 * 1) One-time syscall exit work: 395 * - rseq syscall exit 396 * - audit 397 * - syscall tracing 398 * - ptrace (single stepping) 399 * 400 * 2) Preparatory work 401 * - Exit to user mode loop (common TIF handling). Invokes 402 * arch_exit_to_user_mode_work() for architecture specific TIF work 403 * - Architecture specific one time work arch_exit_to_user_mode_prepare() 404 * - Address limit and lockdep checks 405 * 406 * 3) Final transition (lockdep, tracing, context tracking, RCU), i.e. the 407 * functionality in exit_to_user_mode(). 408 * 409 * This is a combination of syscall_exit_to_user_mode_work() (1,2) and 410 * exit_to_user_mode(). This function is preferred unless there is a 411 * compelling architectural reason to use the separate functions. 412 */ 413void syscall_exit_to_user_mode(struct pt_regs *regs); 414 415/** 416 * irqentry_enter_from_user_mode - Establish state before invoking the irq handler 417 * @regs: Pointer to currents pt_regs 418 * 419 * Invoked from architecture specific entry code with interrupts disabled. 420 * Can only be called when the interrupt entry came from user mode. The 421 * calling code must be non-instrumentable. When the function returns all 422 * state is correct and the subsequent functions can be instrumented. 423 * 424 * The function establishes state (lockdep, RCU (context tracking), tracing) 425 */ 426void irqentry_enter_from_user_mode(struct pt_regs *regs); 427 428/** 429 * irqentry_exit_to_user_mode - Interrupt exit work 430 * @regs: Pointer to current's pt_regs 431 * 432 * Invoked with interrupts disabled and fully valid regs. Returns with all 433 * work handled, interrupts disabled such that the caller can immediately 434 * switch to user mode. Called from architecture specific interrupt 435 * handling code. 436 * 437 * The call order is #2 and #3 as described in syscall_exit_to_user_mode(). 438 * Interrupt exit is not invoking #1 which is the syscall specific one time 439 * work. 440 */ 441void irqentry_exit_to_user_mode(struct pt_regs *regs); 442 443#ifndef irqentry_state 444/** 445 * struct irqentry_state - Opaque object for exception state storage 446 * @exit_rcu: Used exclusively in the irqentry_*() calls; signals whether the 447 * exit path has to invoke ct_irq_exit(). 448 * @lockdep: Used exclusively in the irqentry_nmi_*() calls; ensures that 449 * lockdep state is restored correctly on exit from nmi. 450 * 451 * This opaque object is filled in by the irqentry_*_enter() functions and 452 * must be passed back into the corresponding irqentry_*_exit() functions 453 * when the exception is complete. 454 * 455 * Callers of irqentry_*_[enter|exit]() must consider this structure opaque 456 * and all members private. Descriptions of the members are provided to aid in 457 * the maintenance of the irqentry_*() functions. 458 */ 459typedef struct irqentry_state { 460 union { 461 bool exit_rcu; 462 bool lockdep; 463 }; 464} irqentry_state_t; 465#endif 466 467/** 468 * irqentry_enter - Handle state tracking on ordinary interrupt entries 469 * @regs: Pointer to pt_regs of interrupted context 470 * 471 * Invokes: 472 * - lockdep irqflag state tracking as low level ASM entry disabled 473 * interrupts. 474 * 475 * - Context tracking if the exception hit user mode. 476 * 477 * - The hardirq tracer to keep the state consistent as low level ASM 478 * entry disabled interrupts. 479 * 480 * As a precondition, this requires that the entry came from user mode, 481 * idle, or a kernel context in which RCU is watching. 482 * 483 * For kernel mode entries RCU handling is done conditional. If RCU is 484 * watching then the only RCU requirement is to check whether the tick has 485 * to be restarted. If RCU is not watching then ct_irq_enter() has to be 486 * invoked on entry and ct_irq_exit() on exit. 487 * 488 * Avoiding the ct_irq_enter/exit() calls is an optimization but also 489 * solves the problem of kernel mode pagefaults which can schedule, which 490 * is not possible after invoking ct_irq_enter() without undoing it. 491 * 492 * For user mode entries irqentry_enter_from_user_mode() is invoked to 493 * establish the proper context for NOHZ_FULL. Otherwise scheduling on exit 494 * would not be possible. 495 * 496 * Returns: An opaque object that must be passed to idtentry_exit() 497 */ 498irqentry_state_t noinstr irqentry_enter(struct pt_regs *regs); 499 500/** 501 * irqentry_exit_cond_resched - Conditionally reschedule on return from interrupt 502 * 503 * Conditional reschedule with additional sanity checks. 504 */ 505void raw_irqentry_exit_cond_resched(void); 506#ifdef CONFIG_PREEMPT_DYNAMIC 507#if defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL) 508#define irqentry_exit_cond_resched_dynamic_enabled raw_irqentry_exit_cond_resched 509#define irqentry_exit_cond_resched_dynamic_disabled NULL 510DECLARE_STATIC_CALL(irqentry_exit_cond_resched, raw_irqentry_exit_cond_resched); 511#define irqentry_exit_cond_resched() static_call(irqentry_exit_cond_resched)() 512#elif defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY) 513DECLARE_STATIC_KEY_TRUE(sk_dynamic_irqentry_exit_cond_resched); 514void dynamic_irqentry_exit_cond_resched(void); 515#define irqentry_exit_cond_resched() dynamic_irqentry_exit_cond_resched() 516#endif 517#else /* CONFIG_PREEMPT_DYNAMIC */ 518#define irqentry_exit_cond_resched() raw_irqentry_exit_cond_resched() 519#endif /* CONFIG_PREEMPT_DYNAMIC */ 520 521/** 522 * irqentry_exit - Handle return from exception that used irqentry_enter() 523 * @regs: Pointer to pt_regs (exception entry regs) 524 * @state: Return value from matching call to irqentry_enter() 525 * 526 * Depending on the return target (kernel/user) this runs the necessary 527 * preemption and work checks if possible and required and returns to 528 * the caller with interrupts disabled and no further work pending. 529 * 530 * This is the last action before returning to the low level ASM code which 531 * just needs to return to the appropriate context. 532 * 533 * Counterpart to irqentry_enter(). 534 */ 535void noinstr irqentry_exit(struct pt_regs *regs, irqentry_state_t state); 536 537/** 538 * irqentry_nmi_enter - Handle NMI entry 539 * @regs: Pointer to currents pt_regs 540 * 541 * Similar to irqentry_enter() but taking care of the NMI constraints. 542 */ 543irqentry_state_t noinstr irqentry_nmi_enter(struct pt_regs *regs); 544 545/** 546 * irqentry_nmi_exit - Handle return from NMI handling 547 * @regs: Pointer to pt_regs (NMI entry regs) 548 * @irq_state: Return value from matching call to irqentry_nmi_enter() 549 * 550 * Last action before returning to the low level assembly code. 551 * 552 * Counterpart to irqentry_nmi_enter(). 553 */ 554void noinstr irqentry_nmi_exit(struct pt_regs *regs, irqentry_state_t irq_state); 555 556#endif