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