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