xref: /linux-6.15/arch/s390/kernel/process.c (revision 13cccafe)
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>
332317b07dSMartin Schwidefsky #include <asm/cpu_mf.h>
341da177e4SLinus Torvalds #include <asm/io.h>
351da177e4SLinus Torvalds #include <asm/processor.h>
3627f6b416SMartin Schwidefsky #include <asm/vtimer.h>
37f6e38691SHeiko Carstens #include <asm/exec.h>
381da177e4SLinus Torvalds #include <asm/irq.h>
39f5daba1dSHeiko Carstens #include <asm/nmi.h>
40da7f51c1SHeiko Carstens #include <asm/smp.h>
4178c98f90SMartin Schwidefsky #include <asm/stacktrace.h>
42a0616cdeSDavid Howells #include <asm/switch_to.h>
43e4b8b3f3SJan Glauber #include <asm/runtime_instr.h>
446756dd9bSVasily Gorbik #include <asm/unwind.h>
45a806170eSHeiko Carstens #include "entry.h"
461da177e4SLinus Torvalds 
4756e62a73SSven Schnelle void ret_from_fork(void) asm("ret_from_fork");
481da177e4SLinus Torvalds 
4956e62a73SSven Schnelle void __ret_from_fork(struct task_struct *prev, struct pt_regs *regs)
5056e62a73SSven Schnelle {
5156e62a73SSven Schnelle 	void (*func)(void *arg);
5256e62a73SSven Schnelle 
5356e62a73SSven Schnelle 	schedule_tail(prev);
5456e62a73SSven Schnelle 
5556e62a73SSven Schnelle 	if (!user_mode(regs)) {
5656e62a73SSven Schnelle 		/* Kernel thread */
5756e62a73SSven Schnelle 		func = (void *)regs->gprs[9];
5856e62a73SSven Schnelle 		func((void *)regs->gprs[10]);
5956e62a73SSven Schnelle 	}
6056e62a73SSven Schnelle 	clear_pt_regs_flag(regs, PIF_SYSCALL);
6156e62a73SSven Schnelle 	syscall_exit_to_user_mode(regs);
6256e62a73SSven Schnelle }
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds void flush_thread(void)
651da177e4SLinus Torvalds {
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds 
682317b07dSMartin Schwidefsky void arch_setup_new_exec(void)
692317b07dSMartin Schwidefsky {
702317b07dSMartin Schwidefsky 	if (S390_lowcore.current_pid != current->pid) {
712317b07dSMartin Schwidefsky 		S390_lowcore.current_pid = current->pid;
722317b07dSMartin Schwidefsky 		if (test_facility(40))
732317b07dSMartin Schwidefsky 			lpp(&S390_lowcore.lpp);
742317b07dSMartin Schwidefsky 	}
752317b07dSMartin Schwidefsky }
762317b07dSMartin Schwidefsky 
776a039eabSHendrik Brueckner void arch_release_task_struct(struct task_struct *tsk)
786a039eabSHendrik Brueckner {
798d9047f8SHeiko Carstens 	runtime_instr_release(tsk);
807b83c629SHeiko Carstens 	guarded_storage_release(tsk);
81155e839aSHendrik Brueckner }
82155e839aSHendrik Brueckner 
83155e839aSHendrik Brueckner int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
84155e839aSHendrik Brueckner {
859977e886SHendrik Brueckner 	/*
869977e886SHendrik Brueckner 	 * Save the floating-point or vector register state of the current
87b5510d9bSHendrik Brueckner 	 * task and set the CIF_FPU flag to lazy restore the FPU register
88b5510d9bSHendrik Brueckner 	 * state when returning to user space.
899977e886SHendrik Brueckner 	 */
90d0164ee2SHendrik Brueckner 	save_fpu_regs();
91b5510d9bSHendrik Brueckner 
923f6813b9SMartin Schwidefsky 	memcpy(dst, src, arch_task_struct_size);
933f6813b9SMartin Schwidefsky 	dst->thread.fpu.regs = dst->thread.fpu.fprs;
94*13cccafeSBrian Foster 
95*13cccafeSBrian Foster 	/*
96*13cccafeSBrian Foster 	 * Don't transfer over the runtime instrumentation or the guarded
97*13cccafeSBrian Foster 	 * storage control block pointers. These fields are cleared here instead
98*13cccafeSBrian Foster 	 * of in copy_thread() to avoid premature freeing of associated memory
99*13cccafeSBrian Foster 	 * on fork() failure. Wait to clear the RI flag because ->stack still
100*13cccafeSBrian Foster 	 * refers to the source thread.
101*13cccafeSBrian Foster 	 */
102*13cccafeSBrian Foster 	dst->thread.ri_cb = NULL;
103*13cccafeSBrian Foster 	dst->thread.gs_cb = NULL;
104*13cccafeSBrian Foster 	dst->thread.gs_bc_cb = NULL;
105*13cccafeSBrian Foster 
106155e839aSHendrik Brueckner 	return 0;
1076a039eabSHendrik Brueckner }
1086a039eabSHendrik Brueckner 
109c5febea0SEric W. Biederman int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
1101da177e4SLinus Torvalds {
111c5febea0SEric W. Biederman 	unsigned long clone_flags = args->flags;
112c5febea0SEric W. Biederman 	unsigned long new_stackp = args->stack;
113c5febea0SEric W. Biederman 	unsigned long tls = args->tls;
1141da177e4SLinus Torvalds 	struct fake_frame
1151da177e4SLinus Torvalds 	{
1161da177e4SLinus Torvalds 		struct stack_frame sf;
1171da177e4SLinus Torvalds 		struct pt_regs childregs;
1181da177e4SLinus Torvalds 	} *frame;
1191da177e4SLinus Torvalds 
120c7584fb6SAl Viro 	frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
1211da177e4SLinus Torvalds 	p->thread.ksp = (unsigned long) frame;
122f9a7e025SAl Viro 	/* Save access registers to new thread structure. */
123f9a7e025SAl Viro 	save_access_regs(&p->thread.acrs[0]);
124f9a7e025SAl Viro 	/* start new process with ar4 pointing to the correct address space */
125f9a7e025SAl Viro 	/* Don't copy debug registers */
126f9a7e025SAl Viro 	memset(&p->thread.per_user, 0, sizeof(p->thread.per_user));
127f9a7e025SAl Viro 	memset(&p->thread.per_event, 0, sizeof(p->thread.per_event));
128f9a7e025SAl Viro 	clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
129a1c5befcSHeiko Carstens 	p->thread.per_flags = 0;
130f9a7e025SAl Viro 	/* Initialize per thread user and system timer values */
13190c53e65SMartin Schwidefsky 	p->thread.user_timer = 0;
1323c915bdcSMartin Schwidefsky 	p->thread.guest_timer = 0;
13390c53e65SMartin Schwidefsky 	p->thread.system_timer = 0;
1343c915bdcSMartin Schwidefsky 	p->thread.hardirq_timer = 0;
1353c915bdcSMartin Schwidefsky 	p->thread.softirq_timer = 0;
1360b38b5e1SSven Schnelle 	p->thread.last_break = 1;
1371da177e4SLinus Torvalds 
138f9a7e025SAl Viro 	frame->sf.back_chain = 0;
13956e62a73SSven Schnelle 	frame->sf.gprs[5] = (unsigned long)frame + sizeof(struct stack_frame);
14056e62a73SSven Schnelle 	frame->sf.gprs[6] = (unsigned long)p;
1411da177e4SLinus Torvalds 	/* new return point is ret_from_fork */
1421da177e4SLinus Torvalds 	frame->sf.gprs[8] = (unsigned long)ret_from_fork;
1431da177e4SLinus Torvalds 	/* fake return stack for resume(), don't go back to schedule */
1441da177e4SLinus Torvalds 	frame->sf.gprs[9] = (unsigned long)frame;
1451da177e4SLinus Torvalds 
146f9a7e025SAl Viro 	/* Store access registers to kernel stack of new process. */
1475bd2e97cSEric W. Biederman 	if (unlikely(args->fn)) {
148f9a7e025SAl Viro 		/* kernel thread */
149f9a7e025SAl Viro 		memset(&frame->childregs, 0, sizeof(struct pt_regs));
150e258d719SMartin Schwidefsky 		frame->childregs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_DAT |
151f9a7e025SAl Viro 				PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
152fecc868aSHeiko Carstens 		frame->childregs.psw.addr =
15356e62a73SSven Schnelle 				(unsigned long)__ret_from_fork;
1545bd2e97cSEric W. Biederman 		frame->childregs.gprs[9] = (unsigned long)args->fn;
1555bd2e97cSEric W. Biederman 		frame->childregs.gprs[10] = (unsigned long)args->fn_arg;
156f9a7e025SAl Viro 		frame->childregs.orig_gpr2 = -1;
1573b051e89SSven Schnelle 		frame->childregs.last_break = 1;
158f9a7e025SAl Viro 		return 0;
159f9a7e025SAl Viro 	}
16087f1ca8fSAl Viro 	frame->childregs = *current_pt_regs();
161f9a7e025SAl Viro 	frame->childregs.gprs[2] = 0;	/* child returns 0 on fork. */
162d3a73acbSMartin Schwidefsky 	frame->childregs.flags = 0;
16387f1ca8fSAl Viro 	if (new_stackp)
164f9a7e025SAl Viro 		frame->childregs.gprs[15] = new_stackp;
165*13cccafeSBrian Foster 	/*
166*13cccafeSBrian Foster 	 * Clear the runtime instrumentation flag after the above childregs
167*13cccafeSBrian Foster 	 * copy. The CB pointer was already cleared in arch_dup_task_struct().
168*13cccafeSBrian Foster 	 */
169e4b8b3f3SJan Glauber 	frame->childregs.psw.mask &= ~PSW_MASK_RI;
170e4b8b3f3SJan Glauber 
1711da177e4SLinus Torvalds 	/* Set a new TLS ?  */
1721da177e4SLinus Torvalds 	if (clone_flags & CLONE_SETTLS) {
1737757591aSHeiko Carstens 		if (is_compat_task()) {
17487f1ca8fSAl Viro 			p->thread.acrs[0] = (unsigned int)tls;
1751da177e4SLinus Torvalds 		} else {
17687f1ca8fSAl Viro 			p->thread.acrs[0] = (unsigned int)(tls >> 32);
17787f1ca8fSAl Viro 			p->thread.acrs[1] = (unsigned int)tls;
1781da177e4SLinus Torvalds 		}
1791da177e4SLinus Torvalds 	}
180df29a744SSven Schnelle 	/*
181df29a744SSven Schnelle 	 * s390 stores the svc return address in arch_data when calling
182df29a744SSven Schnelle 	 * sigreturn()/restart_syscall() via vdso. 1 means no valid address
183df29a744SSven Schnelle 	 * stored.
184df29a744SSven Schnelle 	 */
185df29a744SSven Schnelle 	p->restart_block.arch_data = 1;
1861da177e4SLinus Torvalds 	return 0;
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
18956e62a73SSven Schnelle void execve_tail(void)
1901da177e4SLinus Torvalds {
191904818e2SHendrik Brueckner 	current->thread.fpu.fpc = 0;
192e47994ddSHeiko Carstens 	asm volatile("sfpc %0" : : "d" (0));
1931da177e4SLinus Torvalds }
19403ff9a23SMartin Schwidefsky 
19542a20f86SKees Cook unsigned long __get_wchan(struct task_struct *p)
1961da177e4SLinus Torvalds {
1976756dd9bSVasily Gorbik 	struct unwind_state state;
1986756dd9bSVasily Gorbik 	unsigned long ip = 0;
1991da177e4SLinus Torvalds 
20042a20f86SKees Cook 	if (!task_stack_page(p))
2011da177e4SLinus Torvalds 		return 0;
2028769f610SVasily Gorbik 
2038769f610SVasily Gorbik 	if (!try_get_task_stack(p))
2048769f610SVasily Gorbik 		return 0;
2058769f610SVasily Gorbik 
2066756dd9bSVasily Gorbik 	unwind_for_each_frame(&state, p, NULL, 0) {
2076756dd9bSVasily Gorbik 		if (state.stack_info.type != STACK_TYPE_TASK) {
2086756dd9bSVasily Gorbik 			ip = 0;
2096756dd9bSVasily Gorbik 			break;
2108769f610SVasily Gorbik 		}
2116756dd9bSVasily Gorbik 
2126756dd9bSVasily Gorbik 		ip = unwind_get_return_address(&state);
2136756dd9bSVasily Gorbik 		if (!ip)
2146756dd9bSVasily Gorbik 			break;
2156756dd9bSVasily Gorbik 
2166756dd9bSVasily Gorbik 		if (!in_sched_functions(ip))
2176756dd9bSVasily Gorbik 			break;
2188769f610SVasily Gorbik 	}
2196756dd9bSVasily Gorbik 
2208769f610SVasily Gorbik 	put_task_stack(p);
2216756dd9bSVasily Gorbik 	return ip;
2221da177e4SLinus Torvalds }
2239887a1fcSHeiko Carstens 
2249887a1fcSHeiko Carstens unsigned long arch_align_stack(unsigned long sp)
2259887a1fcSHeiko Carstens {
2269887a1fcSHeiko Carstens 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
2279887a1fcSHeiko Carstens 		sp -= get_random_int() & ~PAGE_MASK;
2289887a1fcSHeiko Carstens 	return sp & ~0xf;
2299887a1fcSHeiko Carstens }
23033519182SHeiko Carstens 
23133519182SHeiko Carstens static inline unsigned long brk_rnd(void)
23233519182SHeiko Carstens {
233c7e8b2c2SMartin Schwidefsky 	return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT;
23433519182SHeiko Carstens }
23533519182SHeiko Carstens 
23633519182SHeiko Carstens unsigned long arch_randomize_brk(struct mm_struct *mm)
23733519182SHeiko Carstens {
2389efe4f29SMartin Schwidefsky 	unsigned long ret;
23933519182SHeiko Carstens 
2409efe4f29SMartin Schwidefsky 	ret = PAGE_ALIGN(mm->brk + brk_rnd());
2419efe4f29SMartin Schwidefsky 	return (ret > mm->brk) ? ret : mm->brk;
24233519182SHeiko Carstens }
243