1 /* SPDX-License-Identifier: GPL-2.0 */ 1 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 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 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_ 39 */ 40 #ifndef ARCH_SYSCALL_WORK_EXIT 41 # define ARCH_SYSCALL_WORK_EXIT (0) 42 #endif 43 44 #define SYSCALL_WORK_ENTER (SYSCALL_WORK_ 45 SYSCALL_WORK_ 46 SYSCALL_WORK_ 47 SYSCALL_WORK_ 48 SYSCALL_WORK_ 49 SYSCALL_WORK_ 50 ARCH_SYSCALL_ 51 #define SYSCALL_WORK_EXIT (SYSCALL_WORK_ 52 SYSCALL_WORK_ 53 SYSCALL_WORK_ 54 SYSCALL_WORK_ 55 SYSCALL_WORK_ 56 ARCH_SYSCALL_ 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 63 #endif 64 65 #define EXIT_TO_USER_MODE_WORK 66 (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME 67 _TIF_NEED_RESCHED | _TIF_PATCH_PENDIN 68 ARCH_EXIT_TO_USER_MODE_WORK) 69 70 /** 71 * arch_enter_from_user_mode - Architecture sp 72 * @regs: Pointer to currents pt_regs 73 * 74 * Defaults to an empty implementation. Can be 75 * specific code. 76 * 77 * Invoked from syscall_enter_from_user_mode() 78 * section. Use __always_inline so the compile 79 * and make it instrumentable. 80 */ 81 static __always_inline void arch_enter_from_us 82 83 #ifndef arch_enter_from_user_mode 84 static __always_inline void arch_enter_from_us 85 #endif 86 87 /** 88 * enter_from_user_mode - Establish state when 89 * 90 * Syscall/interrupt entry disables interrupts 91 * interrupts enabled. Also with NO_HZ_FULL RC 92 * 93 * 1) Tell lockdep that interrupts are disable 94 * 2) Invoke context tracking if enabled to re 95 * 3) Trace interrupts off state 96 * 97 * Invoked from architecture specific syscall 98 * disabled. The calling code has to be non-in 99 * function returns all state is correct and i 100 * disabled. The subsequent functions can be i 101 * 102 * This is invoked when there is architecture 103 * done between establishing state and enablin 104 * enable interrupts before invoking syscall_e 105 */ 106 static __always_inline void enter_from_user_mo 107 { 108 arch_enter_from_user_mode(regs); 109 lockdep_hardirqs_off(CALLER_ADDR0); 110 111 CT_WARN_ON(__ct_state() != CT_STATE_US 112 user_exit_irqoff(); 113 114 instrumentation_begin(); 115 kmsan_unpoison_entry_regs(regs); 116 trace_hardirqs_off_finish(); 117 instrumentation_end(); 118 } 119 120 /** 121 * syscall_enter_from_user_mode_prepare - Esta 122 * @regs: Pointer to currents pt_regs 123 * 124 * Invoked from architecture specific syscall 125 * disabled. The calling code has to be non-in 126 * function returns all state is correct, inte 127 * subsequent functions can be instrumented. 128 * 129 * This handles lockdep, RCU (context tracking 130 * the functionality provided by enter_from_us 131 * 132 * This is invoked when there is extra archite 133 * to be done between establishing state and h 134 */ 135 void syscall_enter_from_user_mode_prepare(stru 136 137 long syscall_trace_enter(struct pt_regs *regs, 138 unsigned long work); 139 140 /** 141 * syscall_enter_from_user_mode_work - Check a 142 * a sysca 143 * @regs: Pointer to currents pt_regs 144 * @syscall: The syscall number 145 * 146 * Invoked from architecture specific syscall 147 * enabled after invoking syscall_enter_from_u 148 * architecture specific work. 149 * 150 * Returns: The original or a modified syscall 151 * 152 * If the returned syscall number is -1 then t 153 * skipped. In this case the caller may invoke 154 * syscall_set_return_value() first. If neith 155 * is returned, then the syscall will fail wit 156 * 157 * It handles the following work items: 158 * 159 * 1) syscall_work flag dependent invocations 160 * ptrace_report_syscall_entry(), __secure 161 * 2) Invocation of audit_syscall_entry() 162 */ 163 static __always_inline long syscall_enter_from 164 { 165 unsigned long work = READ_ONCE(current 166 167 if (work & SYSCALL_WORK_ENTER) 168 syscall = syscall_trace_enter( 169 170 return syscall; 171 } 172 173 /** 174 * syscall_enter_from_user_mode - Establish st 175 * before invok 176 * @regs: Pointer to currents pt_regs 177 * @syscall: The syscall number 178 * 179 * Invoked from architecture specific syscall 180 * disabled. The calling code has to be non-in 181 * function returns all state is correct, inte 182 * subsequent functions can be instrumented. 183 * 184 * This is combination of syscall_enter_from_u 185 * syscall_enter_from_user_mode_work(). 186 * 187 * Returns: The original or a modified syscall 188 * syscall_enter_from_user_mode_work() for fur 189 */ 190 static __always_inline long syscall_enter_from 191 { 192 long ret; 193 194 enter_from_user_mode(regs); 195 196 instrumentation_begin(); 197 local_irq_enable(); 198 ret = syscall_enter_from_user_mode_wor 199 instrumentation_end(); 200 201 return ret; 202 } 203 204 /** 205 * local_irq_enable_exit_to_user - Exit to use 206 * @ti_work: Cached TIF flags gathered with 207 * 208 * Defaults to local_irq_enable(). Can be supp 209 * code. 210 */ 211 static inline void local_irq_enable_exit_to_us 212 213 #ifndef local_irq_enable_exit_to_user 214 static inline void local_irq_enable_exit_to_us 215 { 216 local_irq_enable(); 217 } 218 #endif 219 220 /** 221 * local_irq_disable_exit_to_user - Exit to us 222 * 223 * Defaults to local_irq_disable(). Can be sup 224 * code. 225 */ 226 static inline void local_irq_disable_exit_to_u 227 228 #ifndef local_irq_disable_exit_to_user 229 static inline void local_irq_disable_exit_to_u 230 { 231 local_irq_disable(); 232 } 233 #endif 234 235 /** 236 * arch_exit_to_user_mode_work - Architecture 237 * to user mode. 238 * @regs: Pointer to currents pt_regs 239 * @ti_work: Cached TIF flags gathered with 240 * 241 * Invoked from exit_to_user_mode_loop() with 242 * 243 * Defaults to NOOP. Can be supplied by archit 244 */ 245 static inline void arch_exit_to_user_mode_work 246 247 248 #ifndef arch_exit_to_user_mode_work 249 static inline void arch_exit_to_user_mode_work 250 251 { 252 } 253 #endif 254 255 /** 256 * arch_exit_to_user_mode_prepare - Architectu 257 * exit to us 258 * @regs: Pointer to currents pt_regs 259 * @ti_work: Cached TIF flags gathered with 260 * 261 * Invoked from exit_to_user_mode_prepare() wi 262 * function before return. Defaults to NOOP. 263 */ 264 static inline void arch_exit_to_user_mode_prep 265 266 267 #ifndef arch_exit_to_user_mode_prepare 268 static inline void arch_exit_to_user_mode_prep 269 270 { 271 } 272 #endif 273 274 /** 275 * arch_exit_to_user_mode - Architecture speci 276 * exit to user mode. 277 * 278 * Invoked from exit_to_user_mode() with inter 279 * function before return. Defaults to NOOP. 280 * 281 * This needs to be __always_inline because it 282 * invoked after context tracking switched to 283 * 284 * An architecture implementation must not do 285 * etc. The main purpose is for speculation mi 286 */ 287 static __always_inline void arch_exit_to_user_ 288 289 #ifndef arch_exit_to_user_mode 290 static __always_inline void arch_exit_to_user_ 291 #endif 292 293 /** 294 * arch_do_signal_or_restart - Architecture s 295 * @regs: Pointer to currents pt_regs 296 * 297 * Invoked from exit_to_user_mode_loop(). 298 */ 299 void arch_do_signal_or_restart(struct pt_regs 300 301 /** 302 * exit_to_user_mode_loop - do any pending wor 303 */ 304 unsigned long exit_to_user_mode_loop(struct pt 305 unsigned 306 307 /** 308 * exit_to_user_mode_prepare - call exit_to_us 309 * @regs: Pointer to pt_regs on entry st 310 * 311 * 1) check that interrupts are disabled 312 * 2) call tick_nohz_user_enter_prepare() 313 * 3) call exit_to_user_mode_loop() if any fla 314 * EXIT_TO_USER_MODE_WORK are set 315 * 4) check that interrupts are still disabled 316 */ 317 static __always_inline void exit_to_user_mode_ 318 { 319 unsigned long ti_work; 320 321 lockdep_assert_irqs_disabled(); 322 323 /* Flush pending rcuog wakeup before t 324 tick_nohz_user_enter_prepare(); 325 326 ti_work = read_thread_flags(); 327 if (unlikely(ti_work & EXIT_TO_USER_MO 328 ti_work = exit_to_user_mode_lo 329 330 arch_exit_to_user_mode_prepare(regs, t 331 332 /* Ensure that kernel state is sane fo 333 kmap_assert_nomap(); 334 lockdep_assert_irqs_disabled(); 335 lockdep_sys_exit(); 336 } 337 338 /** 339 * exit_to_user_mode - Fixup state when exitin 340 * 341 * Syscall/interrupt exit enables interrupts, 342 * interrupts disabled when this is invoked. A 343 * 344 * 1) Trace interrupts on state 345 * 2) Invoke context tracking if enabled to ad 346 * 3) Invoke architecture specific last minute 347 * mitigations, etc.: arch_exit_to_user_mod 348 * 4) Tell lockdep that interrupts are enabled 349 * 350 * Invoked from architecture specific code whe 351 * is not suitable as the last step before ret 352 * invoked with interrupts disabled and the ca 353 * non-instrumentable. 354 * The caller has to invoke syscall_exit_to_us 355 */ 356 static __always_inline void exit_to_user_mode( 357 { 358 instrumentation_begin(); 359 trace_hardirqs_on_prepare(); 360 lockdep_hardirqs_on_prepare(); 361 instrumentation_end(); 362 363 user_enter_irqoff(); 364 arch_exit_to_user_mode(); 365 lockdep_hardirqs_on(CALLER_ADDR0); 366 } 367 368 /** 369 * syscall_exit_to_user_mode_work - Handle wor 370 * @regs: Pointer to currents pt_regs 371 * 372 * Same as step 1 and 2 of syscall_exit_to_use 373 * exit_to_user_mode() to perform the final tr 374 * 375 * Calling convention is the same as for sysca 376 * returns with all work handled and interrupt 377 * invoke exit_to_user_mode() before actually 378 * make the final state transitions. Interrupt 379 * return from this function and the invocatio 380 */ 381 void syscall_exit_to_user_mode_work(struct pt_ 382 383 /** 384 * syscall_exit_to_user_mode - Handle work bef 385 * @regs: Pointer to currents pt_regs 386 * 387 * Invoked with interrupts enabled and fully v 388 * work handled, interrupts disabled such that 389 * switch to user mode. Called from architectu 390 * from fork code. 391 * 392 * The call order is: 393 * 1) One-time syscall exit work: 394 * - rseq syscall exit 395 * - audit 396 * - syscall tracing 397 * - ptrace (single stepping) 398 * 399 * 2) Preparatory work 400 * - Exit to user mode loop (common TIF h 401 * arch_exit_to_user_mode_work() for ar 402 * - Architecture specific one time work 403 * - Address limit and lockdep checks 404 * 405 * 3) Final transition (lockdep, tracing, con 406 * functionality in exit_to_user_mode(). 407 * 408 * This is a combination of syscall_exit_to_us 409 * exit_to_user_mode(). This function is prefe 410 * compelling architectural reason to use the 411 */ 412 void syscall_exit_to_user_mode(struct pt_regs 413 414 /** 415 * irqentry_enter_from_user_mode - Establish s 416 * @regs: Pointer to currents pt_regs 417 * 418 * Invoked from architecture specific entry co 419 * Can only be called when the interrupt entry 420 * calling code must be non-instrumentable. W 421 * state is correct and the subsequent functio 422 * 423 * The function establishes state (lockdep, RC 424 */ 425 void irqentry_enter_from_user_mode(struct pt_r 426 427 /** 428 * irqentry_exit_to_user_mode - Interrupt exit 429 * @regs: Pointer to current's pt_regs 430 * 431 * Invoked with interrupts disabled and fully 432 * work handled, interrupts disabled such that 433 * switch to user mode. Called from architectu 434 * handling code. 435 * 436 * The call order is #2 and #3 as described in 437 * Interrupt exit is not invoking #1 which is 438 * work. 439 */ 440 void irqentry_exit_to_user_mode(struct pt_regs 441 442 #ifndef irqentry_state 443 /** 444 * struct irqentry_state - Opaque object for e 445 * @exit_rcu: Used exclusively in the irqentry 446 * exit path has to invoke ct_irq_e 447 * @lockdep: Used exclusively in the irqentry_ 448 * lockdep state is restored correct 449 * 450 * This opaque object is filled in by the irqe 451 * must be passed back into the corresponding 452 * when the exception is complete. 453 * 454 * Callers of irqentry_*_[enter|exit]() must c 455 * and all members private. Descriptions of t 456 * the maintenance of the irqentry_*() functio 457 */ 458 typedef struct irqentry_state { 459 union { 460 bool exit_rcu; 461 bool lockdep; 462 }; 463 } irqentry_state_t; 464 #endif 465 466 /** 467 * irqentry_enter - Handle state tracking on o 468 * @regs: Pointer to pt_regs of interrup 469 * 470 * Invokes: 471 * - lockdep irqflag state tracking as low le 472 * interrupts. 473 * 474 * - Context tracking if the exception hit us 475 * 476 * - The hardirq tracer to keep the state con 477 * entry disabled interrupts. 478 * 479 * As a precondition, this requires that the e 480 * idle, or a kernel context in which RCU is w 481 * 482 * For kernel mode entries RCU handling is don 483 * watching then the only RCU requirement is t 484 * to be restarted. If RCU is not watching the 485 * invoked on entry and ct_irq_exit() on exit. 486 * 487 * Avoiding the ct_irq_enter/exit() calls is a 488 * solves the problem of kernel mode pagefault 489 * is not possible after invoking ct_irq_enter 490 * 491 * For user mode entries irqentry_enter_from_u 492 * establish the proper context for NOHZ_FULL. 493 * would not be possible. 494 * 495 * Returns: An opaque object that must be pass 496 */ 497 irqentry_state_t noinstr irqentry_enter(struct 498 499 /** 500 * irqentry_exit_cond_resched - Conditionally 501 * 502 * Conditional reschedule with additional sani 503 */ 504 void raw_irqentry_exit_cond_resched(void); 505 #ifdef CONFIG_PREEMPT_DYNAMIC 506 #if defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL) 507 #define irqentry_exit_cond_resched_dynamic_ena 508 #define irqentry_exit_cond_resched_dynamic_dis 509 DECLARE_STATIC_CALL(irqentry_exit_cond_resched 510 #define irqentry_exit_cond_resched() static 511 #elif defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY) 512 DECLARE_STATIC_KEY_TRUE(sk_dynamic_irqentry_ex 513 void dynamic_irqentry_exit_cond_resched(void); 514 #define irqentry_exit_cond_resched() dynami 515 #endif 516 #else /* CONFIG_PREEMPT_DYNAMIC */ 517 #define irqentry_exit_cond_resched() raw_ir 518 #endif /* CONFIG_PREEMPT_DYNAMIC */ 519 520 /** 521 * irqentry_exit - Handle return from exceptio 522 * @regs: Pointer to pt_regs (exception 523 * @state: Return value from matching cal 524 * 525 * Depending on the return target (kernel/user 526 * preemption and work checks if possible and 527 * the caller with interrupts disabled and no 528 * 529 * This is the last action before returning to 530 * just needs to return to the appropriate con 531 * 532 * Counterpart to irqentry_enter(). 533 */ 534 void noinstr irqentry_exit(struct pt_regs *reg 535 536 /** 537 * irqentry_nmi_enter - Handle NMI entry 538 * @regs: Pointer to currents pt_regs 539 * 540 * Similar to irqentry_enter() but taking care 541 */ 542 irqentry_state_t noinstr irqentry_nmi_enter(st 543 544 /** 545 * irqentry_nmi_exit - Handle return from NMI 546 * @regs: Pointer to pt_regs (NMI entry 547 * @irq_state: Return value from matching cal 548 * 549 * Last action before returning to the low lev 550 * 551 * Counterpart to irqentry_nmi_enter(). 552 */ 553 void noinstr irqentry_nmi_exit(struct pt_regs 554 555 #endif 556
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.