11547db7dSXiu Jianfeng // SPDX-License-Identifier: GPL-2.0-or-later
21547db7dSXiu Jianfeng
31547db7dSXiu Jianfeng #include <linux/compat.h>
41547db7dSXiu Jianfeng #include <linux/context_tracking.h>
5f4a0318fSXiu Jianfeng #include <linux/randomize_kstack.h>
61547db7dSXiu Jianfeng
71547db7dSXiu Jianfeng #include <asm/interrupt.h>
81547db7dSXiu Jianfeng #include <asm/kup.h>
91547db7dSXiu Jianfeng #include <asm/syscall.h>
101547db7dSXiu Jianfeng #include <asm/time.h>
111547db7dSXiu Jianfeng #include <asm/tm.h>
121547db7dSXiu Jianfeng #include <asm/unistd.h>
131547db7dSXiu Jianfeng
141547db7dSXiu Jianfeng
151547db7dSXiu Jianfeng /* Has to run notrace because it is entered not completely "reconciled" */
system_call_exception(struct pt_regs * regs,unsigned long r0)16f8971c62SRohan McLure notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
171547db7dSXiu Jianfeng {
18f4a0318fSXiu Jianfeng long ret;
191547db7dSXiu Jianfeng syscall_fn f;
201547db7dSXiu Jianfeng
211547db7dSXiu Jianfeng kuap_lock();
221547db7dSXiu Jianfeng
23f4a0318fSXiu Jianfeng add_random_kstack_offset();
241547db7dSXiu Jianfeng
251547db7dSXiu Jianfeng if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG))
261547db7dSXiu Jianfeng BUG_ON(irq_soft_mask_return() != IRQS_ALL_DISABLED);
271547db7dSXiu Jianfeng
281547db7dSXiu Jianfeng trace_hardirqs_off(); /* finish reconciling */
291547db7dSXiu Jianfeng
301547db7dSXiu Jianfeng CT_WARN_ON(ct_state() == CONTEXT_KERNEL);
311547db7dSXiu Jianfeng user_exit_irqoff();
321547db7dSXiu Jianfeng
331547db7dSXiu Jianfeng BUG_ON(regs_is_unrecoverable(regs));
341547db7dSXiu Jianfeng BUG_ON(!(regs->msr & MSR_PR));
351547db7dSXiu Jianfeng BUG_ON(arch_irq_disabled_regs(regs));
361547db7dSXiu Jianfeng
371547db7dSXiu Jianfeng #ifdef CONFIG_PPC_PKEY
381547db7dSXiu Jianfeng if (mmu_has_feature(MMU_FTR_PKEY)) {
391547db7dSXiu Jianfeng unsigned long amr, iamr;
401547db7dSXiu Jianfeng bool flush_needed = false;
411547db7dSXiu Jianfeng /*
421547db7dSXiu Jianfeng * When entering from userspace we mostly have the AMR/IAMR
431547db7dSXiu Jianfeng * different from kernel default values. Hence don't compare.
441547db7dSXiu Jianfeng */
451547db7dSXiu Jianfeng amr = mfspr(SPRN_AMR);
461547db7dSXiu Jianfeng iamr = mfspr(SPRN_IAMR);
471547db7dSXiu Jianfeng regs->amr = amr;
481547db7dSXiu Jianfeng regs->iamr = iamr;
49*4589a2b7SChristophe Leroy if (mmu_has_feature(MMU_FTR_KUAP)) {
501547db7dSXiu Jianfeng mtspr(SPRN_AMR, AMR_KUAP_BLOCKED);
511547db7dSXiu Jianfeng flush_needed = true;
521547db7dSXiu Jianfeng }
531547db7dSXiu Jianfeng if (mmu_has_feature(MMU_FTR_BOOK3S_KUEP)) {
541547db7dSXiu Jianfeng mtspr(SPRN_IAMR, AMR_KUEP_BLOCKED);
551547db7dSXiu Jianfeng flush_needed = true;
561547db7dSXiu Jianfeng }
571547db7dSXiu Jianfeng if (flush_needed)
581547db7dSXiu Jianfeng isync();
591547db7dSXiu Jianfeng } else
601547db7dSXiu Jianfeng #endif
611547db7dSXiu Jianfeng kuap_assert_locked();
621547db7dSXiu Jianfeng
631547db7dSXiu Jianfeng booke_restore_dbcr0();
641547db7dSXiu Jianfeng
651547db7dSXiu Jianfeng account_cpu_user_entry();
661547db7dSXiu Jianfeng
671547db7dSXiu Jianfeng account_stolen_time();
681547db7dSXiu Jianfeng
691547db7dSXiu Jianfeng /*
701547db7dSXiu Jianfeng * This is not required for the syscall exit path, but makes the
711547db7dSXiu Jianfeng * stack frame look nicer. If this was initialised in the first stack
721547db7dSXiu Jianfeng * frame, or if the unwinder was taught the first stack frame always
731547db7dSXiu Jianfeng * returns to user with IRQS_ENABLED, this store could be avoided!
741547db7dSXiu Jianfeng */
751547db7dSXiu Jianfeng irq_soft_mask_regs_set_state(regs, IRQS_ENABLED);
761547db7dSXiu Jianfeng
771547db7dSXiu Jianfeng /*
781547db7dSXiu Jianfeng * If system call is called with TM active, set _TIF_RESTOREALL to
791547db7dSXiu Jianfeng * prevent RFSCV being used to return to userspace, because POWER9
801547db7dSXiu Jianfeng * TM implementation has problems with this instruction returning to
811547db7dSXiu Jianfeng * transactional state. Final register values are not relevant because
821547db7dSXiu Jianfeng * the transaction will be aborted upon return anyway. Or in the case
831547db7dSXiu Jianfeng * of unsupported_scv SIGILL fault, the return state does not much
841547db7dSXiu Jianfeng * matter because it's an edge case.
851547db7dSXiu Jianfeng */
861547db7dSXiu Jianfeng if (IS_ENABLED(CONFIG_PPC_TRANSACTIONAL_MEM) &&
871547db7dSXiu Jianfeng unlikely(MSR_TM_TRANSACTIONAL(regs->msr)))
881547db7dSXiu Jianfeng set_bits(_TIF_RESTOREALL, ¤t_thread_info()->flags);
891547db7dSXiu Jianfeng
901547db7dSXiu Jianfeng /*
911547db7dSXiu Jianfeng * If the system call was made with a transaction active, doom it and
921547db7dSXiu Jianfeng * return without performing the system call. Unless it was an
931547db7dSXiu Jianfeng * unsupported scv vector, in which case it's treated like an illegal
941547db7dSXiu Jianfeng * instruction.
951547db7dSXiu Jianfeng */
961547db7dSXiu Jianfeng #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
971547db7dSXiu Jianfeng if (unlikely(MSR_TM_TRANSACTIONAL(regs->msr)) &&
981547db7dSXiu Jianfeng !trap_is_unsupported_scv(regs)) {
991547db7dSXiu Jianfeng /* Enable TM in the kernel, and disable EE (for scv) */
1001547db7dSXiu Jianfeng hard_irq_disable();
1011547db7dSXiu Jianfeng mtmsr(mfmsr() | MSR_TM);
1021547db7dSXiu Jianfeng
1031547db7dSXiu Jianfeng /* tabort, this dooms the transaction, nothing else */
1041547db7dSXiu Jianfeng asm volatile(".long 0x7c00071d | ((%0) << 16)"
1051547db7dSXiu Jianfeng :: "r"(TM_CAUSE_SYSCALL|TM_CAUSE_PERSISTENT));
1061547db7dSXiu Jianfeng
1071547db7dSXiu Jianfeng /*
1081547db7dSXiu Jianfeng * Userspace will never see the return value. Execution will
1091547db7dSXiu Jianfeng * resume after the tbegin. of the aborted transaction with the
1101547db7dSXiu Jianfeng * checkpointed register state. A context switch could occur
1111547db7dSXiu Jianfeng * or signal delivered to the process before resuming the
1121547db7dSXiu Jianfeng * doomed transaction context, but that should all be handled
1131547db7dSXiu Jianfeng * as expected.
1141547db7dSXiu Jianfeng */
1151547db7dSXiu Jianfeng return -ENOSYS;
1161547db7dSXiu Jianfeng }
1171547db7dSXiu Jianfeng #endif // CONFIG_PPC_TRANSACTIONAL_MEM
1181547db7dSXiu Jianfeng
1191547db7dSXiu Jianfeng local_irq_enable();
1201547db7dSXiu Jianfeng
1211547db7dSXiu Jianfeng if (unlikely(read_thread_flags() & _TIF_SYSCALL_DOTRACE)) {
1221547db7dSXiu Jianfeng if (unlikely(trap_is_unsupported_scv(regs))) {
1231547db7dSXiu Jianfeng /* Unsupported scv vector */
1241547db7dSXiu Jianfeng _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1251547db7dSXiu Jianfeng return regs->gpr[3];
1261547db7dSXiu Jianfeng }
1271547db7dSXiu Jianfeng /*
1281547db7dSXiu Jianfeng * We use the return value of do_syscall_trace_enter() as the
1291547db7dSXiu Jianfeng * syscall number. If the syscall was rejected for any reason
1301547db7dSXiu Jianfeng * do_syscall_trace_enter() returns an invalid syscall number
1311547db7dSXiu Jianfeng * and the test against NR_syscalls will fail and the return
1321547db7dSXiu Jianfeng * value to be used is in regs->gpr[3].
1331547db7dSXiu Jianfeng */
1341547db7dSXiu Jianfeng r0 = do_syscall_trace_enter(regs);
1351547db7dSXiu Jianfeng if (unlikely(r0 >= NR_syscalls))
1361547db7dSXiu Jianfeng return regs->gpr[3];
1371547db7dSXiu Jianfeng
1381547db7dSXiu Jianfeng } else if (unlikely(r0 >= NR_syscalls)) {
1391547db7dSXiu Jianfeng if (unlikely(trap_is_unsupported_scv(regs))) {
1401547db7dSXiu Jianfeng /* Unsupported scv vector */
1411547db7dSXiu Jianfeng _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1421547db7dSXiu Jianfeng return regs->gpr[3];
1431547db7dSXiu Jianfeng }
1441547db7dSXiu Jianfeng return -ENOSYS;
1451547db7dSXiu Jianfeng }
1461547db7dSXiu Jianfeng
1471547db7dSXiu Jianfeng /* May be faster to do array_index_nospec? */
1481547db7dSXiu Jianfeng barrier_nospec();
1491547db7dSXiu Jianfeng
1507e92e01bSRohan McLure #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
1517e92e01bSRohan McLure // No COMPAT if we have SYSCALL_WRAPPER, see Kconfig
1527e92e01bSRohan McLure f = (void *)sys_call_table[r0];
1537e92e01bSRohan McLure ret = f(regs);
1547e92e01bSRohan McLure #else
1551547db7dSXiu Jianfeng if (unlikely(is_compat_task())) {
1567e92e01bSRohan McLure unsigned long r3, r4, r5, r6, r7, r8;
1577e92e01bSRohan McLure
1581547db7dSXiu Jianfeng f = (void *)compat_sys_call_table[r0];
1591547db7dSXiu Jianfeng
1607e92e01bSRohan McLure r3 = regs->gpr[3] & 0x00000000ffffffffULL;
1617e92e01bSRohan McLure r4 = regs->gpr[4] & 0x00000000ffffffffULL;
1627e92e01bSRohan McLure r5 = regs->gpr[5] & 0x00000000ffffffffULL;
1637e92e01bSRohan McLure r6 = regs->gpr[6] & 0x00000000ffffffffULL;
1647e92e01bSRohan McLure r7 = regs->gpr[7] & 0x00000000ffffffffULL;
1657e92e01bSRohan McLure r8 = regs->gpr[8] & 0x00000000ffffffffULL;
1661547db7dSXiu Jianfeng
167f4a0318fSXiu Jianfeng ret = f(r3, r4, r5, r6, r7, r8);
1687e92e01bSRohan McLure } else {
1697e92e01bSRohan McLure f = (void *)sys_call_table[r0];
1707e92e01bSRohan McLure
1717e92e01bSRohan McLure ret = f(regs->gpr[3], regs->gpr[4], regs->gpr[5],
1727e92e01bSRohan McLure regs->gpr[6], regs->gpr[7], regs->gpr[8]);
1737e92e01bSRohan McLure }
1747e92e01bSRohan McLure #endif
175f4a0318fSXiu Jianfeng
176f4a0318fSXiu Jianfeng /*
177f4a0318fSXiu Jianfeng * Ultimately, this value will get limited by KSTACK_OFFSET_MAX(),
178f4a0318fSXiu Jianfeng * so the maximum stack offset is 1k bytes (10 bits).
179f4a0318fSXiu Jianfeng *
180f4a0318fSXiu Jianfeng * The actual entropy will be further reduced by the compiler when
181f4a0318fSXiu Jianfeng * applying stack alignment constraints: the powerpc architecture
182f4a0318fSXiu Jianfeng * may have two kinds of stack alignment (16-bytes and 8-bytes).
183f4a0318fSXiu Jianfeng *
184f4a0318fSXiu Jianfeng * So the resulting 6 or 7 bits of entropy is seen in SP[9:4] or SP[9:3].
185f4a0318fSXiu Jianfeng */
186f4a0318fSXiu Jianfeng choose_random_kstack_offset(mftb());
187f4a0318fSXiu Jianfeng
188f4a0318fSXiu Jianfeng return ret;
1891547db7dSXiu Jianfeng }
190