xref: /linux-6.15/arch/s390/kernel/process.c (revision d7f679ec)
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>
342317b07dSMartin Schwidefsky #include <asm/cpu_mf.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 
92*d7f679ecSHeiko Carstens 	*dst = *src;
933f6813b9SMartin Schwidefsky 	dst->thread.fpu.regs = dst->thread.fpu.fprs;
9413cccafeSBrian Foster 
9513cccafeSBrian Foster 	/*
9613cccafeSBrian Foster 	 * Don't transfer over the runtime instrumentation or the guarded
9713cccafeSBrian Foster 	 * storage control block pointers. These fields are cleared here instead
9813cccafeSBrian Foster 	 * of in copy_thread() to avoid premature freeing of associated memory
9913cccafeSBrian Foster 	 * on fork() failure. Wait to clear the RI flag because ->stack still
10013cccafeSBrian Foster 	 * refers to the source thread.
10113cccafeSBrian Foster 	 */
10213cccafeSBrian Foster 	dst->thread.ri_cb = NULL;
10313cccafeSBrian Foster 	dst->thread.gs_cb = NULL;
10413cccafeSBrian Foster 	dst->thread.gs_bc_cb = NULL;
10513cccafeSBrian 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;
139fb77914aSAl Viro 	frame->sf.gprs[11 - 6] = (unsigned long)&frame->childregs;
140fb77914aSAl Viro 	frame->sf.gprs[12 - 6] = (unsigned long)p;
1411da177e4SLinus Torvalds 	/* new return point is ret_from_fork */
142fb77914aSAl Viro 	frame->sf.gprs[14 - 6] = (unsigned long)ret_from_fork;
1431da177e4SLinus Torvalds 	/* fake return stack for resume(), don't go back to schedule */
144fb77914aSAl Viro 	frame->sf.gprs[15 - 6] = (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));
150bb1520d5SAlexander Gordeev 		frame->childregs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_IO |
151bb1520d5SAlexander Gordeev 					    PSW_MASK_EXT | PSW_MASK_MCHECK;
1525bd2e97cSEric W. Biederman 		frame->childregs.gprs[9] = (unsigned long)args->fn;
1535bd2e97cSEric W. Biederman 		frame->childregs.gprs[10] = (unsigned long)args->fn_arg;
154f9a7e025SAl Viro 		frame->childregs.orig_gpr2 = -1;
1553b051e89SSven Schnelle 		frame->childregs.last_break = 1;
156f9a7e025SAl Viro 		return 0;
157f9a7e025SAl Viro 	}
15887f1ca8fSAl Viro 	frame->childregs = *current_pt_regs();
159f9a7e025SAl Viro 	frame->childregs.gprs[2] = 0;	/* child returns 0 on fork. */
160d3a73acbSMartin Schwidefsky 	frame->childregs.flags = 0;
16187f1ca8fSAl Viro 	if (new_stackp)
162f9a7e025SAl Viro 		frame->childregs.gprs[15] = new_stackp;
16313cccafeSBrian Foster 	/*
16413cccafeSBrian Foster 	 * Clear the runtime instrumentation flag after the above childregs
16513cccafeSBrian Foster 	 * copy. The CB pointer was already cleared in arch_dup_task_struct().
16613cccafeSBrian Foster 	 */
167e4b8b3f3SJan Glauber 	frame->childregs.psw.mask &= ~PSW_MASK_RI;
168e4b8b3f3SJan Glauber 
1691da177e4SLinus Torvalds 	/* Set a new TLS ?  */
1701da177e4SLinus Torvalds 	if (clone_flags & CLONE_SETTLS) {
1717757591aSHeiko Carstens 		if (is_compat_task()) {
17287f1ca8fSAl Viro 			p->thread.acrs[0] = (unsigned int)tls;
1731da177e4SLinus Torvalds 		} else {
17487f1ca8fSAl Viro 			p->thread.acrs[0] = (unsigned int)(tls >> 32);
17587f1ca8fSAl Viro 			p->thread.acrs[1] = (unsigned int)tls;
1761da177e4SLinus Torvalds 		}
1771da177e4SLinus Torvalds 	}
178df29a744SSven Schnelle 	/*
179df29a744SSven Schnelle 	 * s390 stores the svc return address in arch_data when calling
180df29a744SSven Schnelle 	 * sigreturn()/restart_syscall() via vdso. 1 means no valid address
181df29a744SSven Schnelle 	 * stored.
182df29a744SSven Schnelle 	 */
183df29a744SSven Schnelle 	p->restart_block.arch_data = 1;
1841da177e4SLinus Torvalds 	return 0;
1851da177e4SLinus Torvalds }
1861da177e4SLinus Torvalds 
18756e62a73SSven Schnelle void execve_tail(void)
1881da177e4SLinus Torvalds {
189904818e2SHendrik Brueckner 	current->thread.fpu.fpc = 0;
190e47994ddSHeiko Carstens 	asm volatile("sfpc %0" : : "d" (0));
1911da177e4SLinus Torvalds }
19203ff9a23SMartin Schwidefsky 
19342a20f86SKees Cook unsigned long __get_wchan(struct task_struct *p)
1941da177e4SLinus Torvalds {
1956756dd9bSVasily Gorbik 	struct unwind_state state;
1966756dd9bSVasily Gorbik 	unsigned long ip = 0;
1971da177e4SLinus Torvalds 
19842a20f86SKees Cook 	if (!task_stack_page(p))
1991da177e4SLinus Torvalds 		return 0;
2008769f610SVasily Gorbik 
2018769f610SVasily Gorbik 	if (!try_get_task_stack(p))
2028769f610SVasily Gorbik 		return 0;
2038769f610SVasily Gorbik 
2046756dd9bSVasily Gorbik 	unwind_for_each_frame(&state, p, NULL, 0) {
2056756dd9bSVasily Gorbik 		if (state.stack_info.type != STACK_TYPE_TASK) {
2066756dd9bSVasily Gorbik 			ip = 0;
2076756dd9bSVasily Gorbik 			break;
2088769f610SVasily Gorbik 		}
2096756dd9bSVasily Gorbik 
2106756dd9bSVasily Gorbik 		ip = unwind_get_return_address(&state);
2116756dd9bSVasily Gorbik 		if (!ip)
2126756dd9bSVasily Gorbik 			break;
2136756dd9bSVasily Gorbik 
2146756dd9bSVasily Gorbik 		if (!in_sched_functions(ip))
2156756dd9bSVasily Gorbik 			break;
2168769f610SVasily Gorbik 	}
2176756dd9bSVasily Gorbik 
2188769f610SVasily Gorbik 	put_task_stack(p);
2196756dd9bSVasily Gorbik 	return ip;
2201da177e4SLinus Torvalds }
2219887a1fcSHeiko Carstens 
2229887a1fcSHeiko Carstens unsigned long arch_align_stack(unsigned long sp)
2239887a1fcSHeiko Carstens {
2249887a1fcSHeiko Carstens 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
2258032bf12SJason A. Donenfeld 		sp -= get_random_u32_below(PAGE_SIZE);
2269887a1fcSHeiko Carstens 	return sp & ~0xf;
2279887a1fcSHeiko Carstens }
22833519182SHeiko Carstens 
22933519182SHeiko Carstens static inline unsigned long brk_rnd(void)
23033519182SHeiko Carstens {
231f743f16cSJason A. Donenfeld 	return (get_random_u16() & BRK_RND_MASK) << PAGE_SHIFT;
23233519182SHeiko Carstens }
23333519182SHeiko Carstens 
23433519182SHeiko Carstens unsigned long arch_randomize_brk(struct mm_struct *mm)
23533519182SHeiko Carstens {
2369efe4f29SMartin Schwidefsky 	unsigned long ret;
23733519182SHeiko Carstens 
2389efe4f29SMartin Schwidefsky 	ret = PAGE_ALIGN(mm->brk + brk_rnd());
2399efe4f29SMartin Schwidefsky 	return (ret > mm->brk) ? ret : mm->brk;
24033519182SHeiko Carstens }
241