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