1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 3cbdc2292SHeiko Carstens * This file handles the architecture dependent parts of process handling. 41da177e4SLinus Torvalds * 5cbdc2292SHeiko Carstens * Copyright IBM Corp. 1999, 2009 6cbdc2292SHeiko Carstens * Author(s): Martin Schwidefsky <[email protected]>, 7cbdc2292SHeiko Carstens * Hartmut Penner <[email protected]>, 8cbdc2292SHeiko Carstens * Denis Joseph Barrow, 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 11ca21872eSHeiko Carstens #include <linux/elf-randomize.h> 121da177e4SLinus Torvalds #include <linux/compiler.h> 131da177e4SLinus Torvalds #include <linux/cpu.h> 141da177e4SLinus Torvalds #include <linux/sched.h> 15b17b0153SIngo Molnar #include <linux/sched/debug.h> 1629930025SIngo Molnar #include <linux/sched/task.h> 1768db0cf1SIngo Molnar #include <linux/sched/task_stack.h> 181da177e4SLinus Torvalds #include <linux/kernel.h> 191da177e4SLinus Torvalds #include <linux/mm.h> 20638ad34aSMartin Schwidefsky #include <linux/elfcore.h> 211da177e4SLinus Torvalds #include <linux/smp.h> 225a0e3ad6STejun Heo #include <linux/slab.h> 231da177e4SLinus Torvalds #include <linux/interrupt.h> 245a62b192SHeiko Carstens #include <linux/tick.h> 259887a1fcSHeiko Carstens #include <linux/personality.h> 2626689452SHeiko Carstens #include <linux/syscalls.h> 273e86a8c6SHeiko Carstens #include <linux/compat.h> 28860dba45SMartin Schwidefsky #include <linux/kprobes.h> 299887a1fcSHeiko Carstens #include <linux/random.h> 303994a52bSPaul Gortmaker #include <linux/export.h> 310ac27779SHendrik Brueckner #include <linux/init_task.h> 3256e62a73SSven Schnelle #include <linux/entry-common.h> 33b378a982SHeiko Carstens #include <linux/io.h> 34340750c1SHeiko Carstens #include <asm/guarded_storage.h> 35340750c1SHeiko Carstens #include <asm/access-regs.h> 36340750c1SHeiko Carstens #include <asm/switch_to.h> 372317b07dSMartin Schwidefsky #include <asm/cpu_mf.h> 381da177e4SLinus Torvalds #include <asm/processor.h> 39340750c1SHeiko Carstens #include <asm/ptrace.h> 4027f6b416SMartin Schwidefsky #include <asm/vtimer.h> 41f6e38691SHeiko Carstens #include <asm/exec.h> 42fd2527f2SHeiko Carstens #include <asm/fpu.h> 431da177e4SLinus Torvalds #include <asm/irq.h> 44f5daba1dSHeiko Carstens #include <asm/nmi.h> 45da7f51c1SHeiko Carstens #include <asm/smp.h> 4678c98f90SMartin Schwidefsky #include <asm/stacktrace.h> 47e4b8b3f3SJan Glauber #include <asm/runtime_instr.h> 486756dd9bSVasily Gorbik #include <asm/unwind.h> 49a806170eSHeiko Carstens #include "entry.h" 501da177e4SLinus Torvalds 5156e62a73SSven Schnelle void ret_from_fork(void) asm("ret_from_fork"); 521da177e4SLinus Torvalds 5356e62a73SSven Schnelle void __ret_from_fork(struct task_struct *prev, struct pt_regs *regs) 5456e62a73SSven Schnelle { 5556e62a73SSven Schnelle void (*func)(void *arg); 5656e62a73SSven Schnelle 5756e62a73SSven Schnelle schedule_tail(prev); 5856e62a73SSven Schnelle 5956e62a73SSven Schnelle if (!user_mode(regs)) { 6056e62a73SSven Schnelle /* Kernel thread */ 6156e62a73SSven Schnelle func = (void *)regs->gprs[9]; 6256e62a73SSven Schnelle func((void *)regs->gprs[10]); 6356e62a73SSven Schnelle } 6456e62a73SSven Schnelle clear_pt_regs_flag(regs, PIF_SYSCALL); 6556e62a73SSven Schnelle syscall_exit_to_user_mode(regs); 6656e62a73SSven Schnelle } 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds void flush_thread(void) 691da177e4SLinus Torvalds { 701da177e4SLinus Torvalds } 711da177e4SLinus Torvalds 722317b07dSMartin Schwidefsky void arch_setup_new_exec(void) 732317b07dSMartin Schwidefsky { 742317b07dSMartin Schwidefsky if (S390_lowcore.current_pid != current->pid) { 752317b07dSMartin Schwidefsky S390_lowcore.current_pid = current->pid; 762317b07dSMartin Schwidefsky if (test_facility(40)) 772317b07dSMartin Schwidefsky lpp(&S390_lowcore.lpp); 782317b07dSMartin Schwidefsky } 792317b07dSMartin Schwidefsky } 802317b07dSMartin Schwidefsky 816a039eabSHendrik Brueckner void arch_release_task_struct(struct task_struct *tsk) 826a039eabSHendrik Brueckner { 838d9047f8SHeiko Carstens runtime_instr_release(tsk); 847b83c629SHeiko Carstens guarded_storage_release(tsk); 85155e839aSHendrik Brueckner } 86155e839aSHendrik Brueckner 87155e839aSHendrik Brueckner int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) 88155e839aSHendrik Brueckner { 899977e886SHendrik Brueckner /* 909977e886SHendrik Brueckner * Save the floating-point or vector register state of the current 91419abc4dSHeiko Carstens * task and set the TIF_FPU flag to lazy restore the FPU register 92b5510d9bSHendrik Brueckner * state when returning to user space. 939977e886SHendrik Brueckner */ 94*87c5c700SHeiko Carstens save_user_fpu_regs(); 95b5510d9bSHendrik Brueckner 96d7f679ecSHeiko Carstens *dst = *src; 97*87c5c700SHeiko Carstens dst->thread.ufpu.regs = dst->thread.ufpu.fprs; 9813cccafeSBrian Foster 9913cccafeSBrian Foster /* 10013cccafeSBrian Foster * Don't transfer over the runtime instrumentation or the guarded 10113cccafeSBrian Foster * storage control block pointers. These fields are cleared here instead 10213cccafeSBrian Foster * of in copy_thread() to avoid premature freeing of associated memory 10313cccafeSBrian Foster * on fork() failure. Wait to clear the RI flag because ->stack still 10413cccafeSBrian Foster * refers to the source thread. 10513cccafeSBrian Foster */ 10613cccafeSBrian Foster dst->thread.ri_cb = NULL; 10713cccafeSBrian Foster dst->thread.gs_cb = NULL; 10813cccafeSBrian Foster dst->thread.gs_bc_cb = NULL; 10913cccafeSBrian Foster 110155e839aSHendrik Brueckner return 0; 1116a039eabSHendrik Brueckner } 1126a039eabSHendrik Brueckner 113c5febea0SEric W. Biederman int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) 1141da177e4SLinus Torvalds { 115c5febea0SEric W. Biederman unsigned long clone_flags = args->flags; 116c5febea0SEric W. Biederman unsigned long new_stackp = args->stack; 117c5febea0SEric W. Biederman unsigned long tls = args->tls; 1181da177e4SLinus Torvalds struct fake_frame 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds struct stack_frame sf; 1211da177e4SLinus Torvalds struct pt_regs childregs; 1221da177e4SLinus Torvalds } *frame; 1231da177e4SLinus Torvalds 124c7584fb6SAl Viro frame = container_of(task_pt_regs(p), struct fake_frame, childregs); 1251da177e4SLinus Torvalds p->thread.ksp = (unsigned long) frame; 126f9a7e025SAl Viro /* Save access registers to new thread structure. */ 127f9a7e025SAl Viro save_access_regs(&p->thread.acrs[0]); 128f9a7e025SAl Viro /* start new process with ar4 pointing to the correct address space */ 129f9a7e025SAl Viro /* Don't copy debug registers */ 130f9a7e025SAl Viro memset(&p->thread.per_user, 0, sizeof(p->thread.per_user)); 131f9a7e025SAl Viro memset(&p->thread.per_event, 0, sizeof(p->thread.per_event)); 132f9a7e025SAl Viro clear_tsk_thread_flag(p, TIF_SINGLE_STEP); 133a1c5befcSHeiko Carstens p->thread.per_flags = 0; 134f9a7e025SAl Viro /* Initialize per thread user and system timer values */ 13590c53e65SMartin Schwidefsky p->thread.user_timer = 0; 1363c915bdcSMartin Schwidefsky p->thread.guest_timer = 0; 13790c53e65SMartin Schwidefsky p->thread.system_timer = 0; 1383c915bdcSMartin Schwidefsky p->thread.hardirq_timer = 0; 1393c915bdcSMartin Schwidefsky p->thread.softirq_timer = 0; 1400b38b5e1SSven Schnelle p->thread.last_break = 1; 1411da177e4SLinus Torvalds 142f9a7e025SAl Viro frame->sf.back_chain = 0; 143fb77914aSAl Viro frame->sf.gprs[11 - 6] = (unsigned long)&frame->childregs; 144fb77914aSAl Viro frame->sf.gprs[12 - 6] = (unsigned long)p; 1451da177e4SLinus Torvalds /* new return point is ret_from_fork */ 146fb77914aSAl Viro frame->sf.gprs[14 - 6] = (unsigned long)ret_from_fork; 1471da177e4SLinus Torvalds /* fake return stack for resume(), don't go back to schedule */ 148fb77914aSAl Viro frame->sf.gprs[15 - 6] = (unsigned long)frame; 1491da177e4SLinus Torvalds 150f9a7e025SAl Viro /* Store access registers to kernel stack of new process. */ 1515bd2e97cSEric W. Biederman if (unlikely(args->fn)) { 152f9a7e025SAl Viro /* kernel thread */ 153f9a7e025SAl Viro memset(&frame->childregs, 0, sizeof(struct pt_regs)); 154bb1520d5SAlexander Gordeev frame->childregs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_IO | 155bb1520d5SAlexander Gordeev PSW_MASK_EXT | PSW_MASK_MCHECK; 1565bd2e97cSEric W. Biederman frame->childregs.gprs[9] = (unsigned long)args->fn; 1575bd2e97cSEric W. Biederman frame->childregs.gprs[10] = (unsigned long)args->fn_arg; 158f9a7e025SAl Viro frame->childregs.orig_gpr2 = -1; 1593b051e89SSven Schnelle frame->childregs.last_break = 1; 160f9a7e025SAl Viro return 0; 161f9a7e025SAl Viro } 16287f1ca8fSAl Viro frame->childregs = *current_pt_regs(); 163f9a7e025SAl Viro frame->childregs.gprs[2] = 0; /* child returns 0 on fork. */ 164d3a73acbSMartin Schwidefsky frame->childregs.flags = 0; 16587f1ca8fSAl Viro if (new_stackp) 166f9a7e025SAl Viro frame->childregs.gprs[15] = new_stackp; 16713cccafeSBrian Foster /* 16813cccafeSBrian Foster * Clear the runtime instrumentation flag after the above childregs 16913cccafeSBrian Foster * copy. The CB pointer was already cleared in arch_dup_task_struct(). 17013cccafeSBrian Foster */ 171e4b8b3f3SJan Glauber frame->childregs.psw.mask &= ~PSW_MASK_RI; 172e4b8b3f3SJan Glauber 1731da177e4SLinus Torvalds /* Set a new TLS ? */ 1741da177e4SLinus Torvalds if (clone_flags & CLONE_SETTLS) { 1757757591aSHeiko Carstens if (is_compat_task()) { 17687f1ca8fSAl Viro p->thread.acrs[0] = (unsigned int)tls; 1771da177e4SLinus Torvalds } else { 17887f1ca8fSAl Viro p->thread.acrs[0] = (unsigned int)(tls >> 32); 17987f1ca8fSAl Viro p->thread.acrs[1] = (unsigned int)tls; 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds } 182df29a744SSven Schnelle /* 183df29a744SSven Schnelle * s390 stores the svc return address in arch_data when calling 184df29a744SSven Schnelle * sigreturn()/restart_syscall() via vdso. 1 means no valid address 185df29a744SSven Schnelle * stored. 186df29a744SSven Schnelle */ 187df29a744SSven Schnelle p->restart_block.arch_data = 1; 1881da177e4SLinus Torvalds return 0; 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds 19156e62a73SSven Schnelle void execve_tail(void) 1921da177e4SLinus Torvalds { 193*87c5c700SHeiko Carstens current->thread.ufpu.fpc = 0; 194f4e3de75SHeiko Carstens fpu_sfpc(0); 1951da177e4SLinus Torvalds } 19603ff9a23SMartin Schwidefsky 197340750c1SHeiko Carstens struct task_struct *__switch_to(struct task_struct *prev, struct task_struct *next) 198340750c1SHeiko Carstens { 199*87c5c700SHeiko Carstens save_user_fpu_regs(); 200340750c1SHeiko Carstens save_access_regs(&prev->thread.acrs[0]); 201340750c1SHeiko Carstens save_ri_cb(prev->thread.ri_cb); 202340750c1SHeiko Carstens save_gs_cb(prev->thread.gs_cb); 203340750c1SHeiko Carstens update_cr_regs(next); 204340750c1SHeiko Carstens restore_access_regs(&next->thread.acrs[0]); 205340750c1SHeiko Carstens restore_ri_cb(next->thread.ri_cb, prev->thread.ri_cb); 206340750c1SHeiko Carstens restore_gs_cb(next->thread.gs_cb); 207340750c1SHeiko Carstens return __switch_to_asm(prev, next); 208340750c1SHeiko Carstens } 209340750c1SHeiko Carstens 21042a20f86SKees Cook unsigned long __get_wchan(struct task_struct *p) 2111da177e4SLinus Torvalds { 2126756dd9bSVasily Gorbik struct unwind_state state; 2136756dd9bSVasily Gorbik unsigned long ip = 0; 2141da177e4SLinus Torvalds 21542a20f86SKees Cook if (!task_stack_page(p)) 2161da177e4SLinus Torvalds return 0; 2178769f610SVasily Gorbik 2188769f610SVasily Gorbik if (!try_get_task_stack(p)) 2198769f610SVasily Gorbik return 0; 2208769f610SVasily Gorbik 2216756dd9bSVasily Gorbik unwind_for_each_frame(&state, p, NULL, 0) { 2226756dd9bSVasily Gorbik if (state.stack_info.type != STACK_TYPE_TASK) { 2236756dd9bSVasily Gorbik ip = 0; 2246756dd9bSVasily Gorbik break; 2258769f610SVasily Gorbik } 2266756dd9bSVasily Gorbik 2276756dd9bSVasily Gorbik ip = unwind_get_return_address(&state); 2286756dd9bSVasily Gorbik if (!ip) 2296756dd9bSVasily Gorbik break; 2306756dd9bSVasily Gorbik 2316756dd9bSVasily Gorbik if (!in_sched_functions(ip)) 2326756dd9bSVasily Gorbik break; 2338769f610SVasily Gorbik } 2346756dd9bSVasily Gorbik 2358769f610SVasily Gorbik put_task_stack(p); 2366756dd9bSVasily Gorbik return ip; 2371da177e4SLinus Torvalds } 2389887a1fcSHeiko Carstens 2399887a1fcSHeiko Carstens unsigned long arch_align_stack(unsigned long sp) 2409887a1fcSHeiko Carstens { 2419887a1fcSHeiko Carstens if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space) 2428032bf12SJason A. Donenfeld sp -= get_random_u32_below(PAGE_SIZE); 2439887a1fcSHeiko Carstens return sp & ~0xf; 2449887a1fcSHeiko Carstens } 24533519182SHeiko Carstens 24633519182SHeiko Carstens static inline unsigned long brk_rnd(void) 24733519182SHeiko Carstens { 248f743f16cSJason A. Donenfeld return (get_random_u16() & BRK_RND_MASK) << PAGE_SHIFT; 24933519182SHeiko Carstens } 25033519182SHeiko Carstens 25133519182SHeiko Carstens unsigned long arch_randomize_brk(struct mm_struct *mm) 25233519182SHeiko Carstens { 2539efe4f29SMartin Schwidefsky unsigned long ret; 25433519182SHeiko Carstens 2559efe4f29SMartin Schwidefsky ret = PAGE_ALIGN(mm->brk + brk_rnd()); 2569efe4f29SMartin Schwidefsky return (ret > mm->brk) ? ret : mm->brk; 25733519182SHeiko Carstens } 258