1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/compat.h>
4 #include <linux/context_tracking.h>
5 #include <linux/randomize_kstack.h>
7 #include <asm/interrupt.h>
9 #include <asm/syscall.h>
12 #include <asm/unistd.h>
15 /* Has to run notrace because it is entered not completely "reconciled" */
16 notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
23 add_random_kstack_offset();
25 if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG))
26 BUG_ON(irq_soft_mask_return() != IRQS_ALL_DISABLED);
28 trace_hardirqs_off(); /* finish reconciling */
30 CT_WARN_ON(ct_state() == CONTEXT_KERNEL);
33 BUG_ON(regs_is_unrecoverable(regs));
34 BUG_ON(!(regs->msr & MSR_PR));
35 BUG_ON(arch_irq_disabled_regs(regs));
37 #ifdef CONFIG_PPC_PKEY
38 if (mmu_has_feature(MMU_FTR_PKEY)) {
39 unsigned long amr, iamr;
40 bool flush_needed = false;
42 * When entering from userspace we mostly have the AMR/IAMR
43 * different from kernel default values. Hence don't compare.
45 amr = mfspr(SPRN_AMR);
46 iamr = mfspr(SPRN_IAMR);
49 if (mmu_has_feature(MMU_FTR_KUAP)) {
50 mtspr(SPRN_AMR, AMR_KUAP_BLOCKED);
53 if (mmu_has_feature(MMU_FTR_BOOK3S_KUEP)) {
54 mtspr(SPRN_IAMR, AMR_KUEP_BLOCKED);
63 booke_restore_dbcr0();
65 account_cpu_user_entry();
67 account_stolen_time();
70 * This is not required for the syscall exit path, but makes the
71 * stack frame look nicer. If this was initialised in the first stack
72 * frame, or if the unwinder was taught the first stack frame always
73 * returns to user with IRQS_ENABLED, this store could be avoided!
75 irq_soft_mask_regs_set_state(regs, IRQS_ENABLED);
78 * If system call is called with TM active, set _TIF_RESTOREALL to
79 * prevent RFSCV being used to return to userspace, because POWER9
80 * TM implementation has problems with this instruction returning to
81 * transactional state. Final register values are not relevant because
82 * the transaction will be aborted upon return anyway. Or in the case
83 * of unsupported_scv SIGILL fault, the return state does not much
84 * matter because it's an edge case.
86 if (IS_ENABLED(CONFIG_PPC_TRANSACTIONAL_MEM) &&
87 unlikely(MSR_TM_TRANSACTIONAL(regs->msr)))
88 set_bits(_TIF_RESTOREALL, ¤t_thread_info()->flags);
91 * If the system call was made with a transaction active, doom it and
92 * return without performing the system call. Unless it was an
93 * unsupported scv vector, in which case it's treated like an illegal
96 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
97 if (unlikely(MSR_TM_TRANSACTIONAL(regs->msr)) &&
98 !trap_is_unsupported_scv(regs)) {
99 /* Enable TM in the kernel, and disable EE (for scv) */
101 mtmsr(mfmsr() | MSR_TM);
103 /* tabort, this dooms the transaction, nothing else */
104 asm volatile(".long 0x7c00071d | ((%0) << 16)"
105 :: "r"(TM_CAUSE_SYSCALL|TM_CAUSE_PERSISTENT));
108 * Userspace will never see the return value. Execution will
109 * resume after the tbegin. of the aborted transaction with the
110 * checkpointed register state. A context switch could occur
111 * or signal delivered to the process before resuming the
112 * doomed transaction context, but that should all be handled
117 #endif // CONFIG_PPC_TRANSACTIONAL_MEM
121 if (unlikely(read_thread_flags() & _TIF_SYSCALL_DOTRACE)) {
122 if (unlikely(trap_is_unsupported_scv(regs))) {
123 /* Unsupported scv vector */
124 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
128 * We use the return value of do_syscall_trace_enter() as the
129 * syscall number. If the syscall was rejected for any reason
130 * do_syscall_trace_enter() returns an invalid syscall number
131 * and the test against NR_syscalls will fail and the return
132 * value to be used is in regs->gpr[3].
134 r0 = do_syscall_trace_enter(regs);
135 if (unlikely(r0 >= NR_syscalls))
138 } else if (unlikely(r0 >= NR_syscalls)) {
139 if (unlikely(trap_is_unsupported_scv(regs))) {
140 /* Unsupported scv vector */
141 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
147 /* May be faster to do array_index_nospec? */
150 #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
151 // No COMPAT if we have SYSCALL_WRAPPER, see Kconfig
152 f = (void *)sys_call_table[r0];
155 if (unlikely(is_compat_task())) {
156 unsigned long r3, r4, r5, r6, r7, r8;
158 f = (void *)compat_sys_call_table[r0];
160 r3 = regs->gpr[3] & 0x00000000ffffffffULL;
161 r4 = regs->gpr[4] & 0x00000000ffffffffULL;
162 r5 = regs->gpr[5] & 0x00000000ffffffffULL;
163 r6 = regs->gpr[6] & 0x00000000ffffffffULL;
164 r7 = regs->gpr[7] & 0x00000000ffffffffULL;
165 r8 = regs->gpr[8] & 0x00000000ffffffffULL;
167 ret = f(r3, r4, r5, r6, r7, r8);
169 f = (void *)sys_call_table[r0];
171 ret = f(regs->gpr[3], regs->gpr[4], regs->gpr[5],
172 regs->gpr[6], regs->gpr[7], regs->gpr[8]);
177 * Ultimately, this value will get limited by KSTACK_OFFSET_MAX(),
178 * so the maximum stack offset is 1k bytes (10 bits).
180 * The actual entropy will be further reduced by the compiler when
181 * applying stack alignment constraints: the powerpc architecture
182 * may have two kinds of stack alignment (16-bytes and 8-bytes).
184 * So the resulting 6 or 7 bits of entropy is seen in SP[9:4] or SP[9:3].
186 choose_random_kstack_offset(mftb());