xref: /linux-6.15/arch/s390/kernel/process.c (revision 208da1d5)
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 
__ret_from_fork(struct task_struct * prev,struct pt_regs * regs)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 
flush_thread(void)681da177e4SLinus Torvalds void flush_thread(void)
691da177e4SLinus Torvalds {
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
arch_setup_new_exec(void)722317b07dSMartin Schwidefsky void arch_setup_new_exec(void)
732317b07dSMartin Schwidefsky {
74*208da1d5SSven Schnelle 	if (get_lowcore()->current_pid != current->pid) {
75*208da1d5SSven Schnelle 		get_lowcore()->current_pid = current->pid;
762317b07dSMartin Schwidefsky 		if (test_facility(40))
77*208da1d5SSven Schnelle 			lpp(&get_lowcore()->lpp);
782317b07dSMartin Schwidefsky 	}
792317b07dSMartin Schwidefsky }
802317b07dSMartin Schwidefsky 
arch_release_task_struct(struct task_struct * tsk)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 
arch_dup_task_struct(struct task_struct * dst,struct task_struct * src)87155e839aSHendrik Brueckner int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
88155e839aSHendrik Brueckner {
8987c5c700SHeiko Carstens 	save_user_fpu_regs();
90b5510d9bSHendrik Brueckner 
91d7f679ecSHeiko Carstens 	*dst = *src;
924eed43deSHeiko Carstens 	dst->thread.kfpu_flags = 0;
9313cccafeSBrian Foster 
9413cccafeSBrian Foster 	/*
9513cccafeSBrian Foster 	 * Don't transfer over the runtime instrumentation or the guarded
9613cccafeSBrian Foster 	 * storage control block pointers. These fields are cleared here instead
9713cccafeSBrian Foster 	 * of in copy_thread() to avoid premature freeing of associated memory
9813cccafeSBrian Foster 	 * on fork() failure. Wait to clear the RI flag because ->stack still
9913cccafeSBrian Foster 	 * refers to the source thread.
10013cccafeSBrian Foster 	 */
10113cccafeSBrian Foster 	dst->thread.ri_cb = NULL;
10213cccafeSBrian Foster 	dst->thread.gs_cb = NULL;
10313cccafeSBrian Foster 	dst->thread.gs_bc_cb = NULL;
10413cccafeSBrian Foster 
105155e839aSHendrik Brueckner 	return 0;
1066a039eabSHendrik Brueckner }
1076a039eabSHendrik Brueckner 
copy_thread(struct task_struct * p,const struct kernel_clone_args * args)108c5febea0SEric W. Biederman int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
1091da177e4SLinus Torvalds {
110c5febea0SEric W. Biederman 	unsigned long clone_flags = args->flags;
111c5febea0SEric W. Biederman 	unsigned long new_stackp = args->stack;
112c5febea0SEric W. Biederman 	unsigned long tls = args->tls;
1131da177e4SLinus Torvalds 	struct fake_frame
1141da177e4SLinus Torvalds 	{
1151da177e4SLinus Torvalds 		struct stack_frame sf;
1161da177e4SLinus Torvalds 		struct pt_regs childregs;
1171da177e4SLinus Torvalds 	} *frame;
1181da177e4SLinus Torvalds 
119c7584fb6SAl Viro 	frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
1201da177e4SLinus Torvalds 	p->thread.ksp = (unsigned long) frame;
121f9a7e025SAl Viro 	/* Save access registers to new thread structure. */
122f9a7e025SAl Viro 	save_access_regs(&p->thread.acrs[0]);
123f9a7e025SAl Viro 	/* start new process with ar4 pointing to the correct address space */
124f9a7e025SAl Viro 	/* Don't copy debug registers */
125f9a7e025SAl Viro 	memset(&p->thread.per_user, 0, sizeof(p->thread.per_user));
126f9a7e025SAl Viro 	memset(&p->thread.per_event, 0, sizeof(p->thread.per_event));
127f9a7e025SAl Viro 	clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
128a1c5befcSHeiko Carstens 	p->thread.per_flags = 0;
129f9a7e025SAl Viro 	/* Initialize per thread user and system timer values */
13090c53e65SMartin Schwidefsky 	p->thread.user_timer = 0;
1313c915bdcSMartin Schwidefsky 	p->thread.guest_timer = 0;
13290c53e65SMartin Schwidefsky 	p->thread.system_timer = 0;
1333c915bdcSMartin Schwidefsky 	p->thread.hardirq_timer = 0;
1343c915bdcSMartin Schwidefsky 	p->thread.softirq_timer = 0;
1350b38b5e1SSven Schnelle 	p->thread.last_break = 1;
1361da177e4SLinus Torvalds 
137f9a7e025SAl Viro 	frame->sf.back_chain = 0;
138fb77914aSAl Viro 	frame->sf.gprs[11 - 6] = (unsigned long)&frame->childregs;
139fb77914aSAl Viro 	frame->sf.gprs[12 - 6] = (unsigned long)p;
1401da177e4SLinus Torvalds 	/* new return point is ret_from_fork */
141fb77914aSAl Viro 	frame->sf.gprs[14 - 6] = (unsigned long)ret_from_fork;
1421da177e4SLinus Torvalds 	/* fake return stack for resume(), don't go back to schedule */
143fb77914aSAl Viro 	frame->sf.gprs[15 - 6] = (unsigned long)frame;
1441da177e4SLinus Torvalds 
145f9a7e025SAl Viro 	/* Store access registers to kernel stack of new process. */
1465bd2e97cSEric W. Biederman 	if (unlikely(args->fn)) {
147f9a7e025SAl Viro 		/* kernel thread */
148f9a7e025SAl Viro 		memset(&frame->childregs, 0, sizeof(struct pt_regs));
149bb1520d5SAlexander Gordeev 		frame->childregs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_IO |
150bb1520d5SAlexander Gordeev 					    PSW_MASK_EXT | PSW_MASK_MCHECK;
1515bd2e97cSEric W. Biederman 		frame->childregs.gprs[9] = (unsigned long)args->fn;
1525bd2e97cSEric W. Biederman 		frame->childregs.gprs[10] = (unsigned long)args->fn_arg;
153f9a7e025SAl Viro 		frame->childregs.orig_gpr2 = -1;
1543b051e89SSven Schnelle 		frame->childregs.last_break = 1;
155f9a7e025SAl Viro 		return 0;
156f9a7e025SAl Viro 	}
15787f1ca8fSAl Viro 	frame->childregs = *current_pt_regs();
158f9a7e025SAl Viro 	frame->childregs.gprs[2] = 0;	/* child returns 0 on fork. */
159d3a73acbSMartin Schwidefsky 	frame->childregs.flags = 0;
16087f1ca8fSAl Viro 	if (new_stackp)
161f9a7e025SAl Viro 		frame->childregs.gprs[15] = new_stackp;
16213cccafeSBrian Foster 	/*
16313cccafeSBrian Foster 	 * Clear the runtime instrumentation flag after the above childregs
16413cccafeSBrian Foster 	 * copy. The CB pointer was already cleared in arch_dup_task_struct().
16513cccafeSBrian Foster 	 */
166e4b8b3f3SJan Glauber 	frame->childregs.psw.mask &= ~PSW_MASK_RI;
167e4b8b3f3SJan Glauber 
1681da177e4SLinus Torvalds 	/* Set a new TLS ?  */
1691da177e4SLinus Torvalds 	if (clone_flags & CLONE_SETTLS) {
1707757591aSHeiko Carstens 		if (is_compat_task()) {
17187f1ca8fSAl Viro 			p->thread.acrs[0] = (unsigned int)tls;
1721da177e4SLinus Torvalds 		} else {
17387f1ca8fSAl Viro 			p->thread.acrs[0] = (unsigned int)(tls >> 32);
17487f1ca8fSAl Viro 			p->thread.acrs[1] = (unsigned int)tls;
1751da177e4SLinus Torvalds 		}
1761da177e4SLinus Torvalds 	}
177df29a744SSven Schnelle 	/*
178df29a744SSven Schnelle 	 * s390 stores the svc return address in arch_data when calling
179df29a744SSven Schnelle 	 * sigreturn()/restart_syscall() via vdso. 1 means no valid address
180df29a744SSven Schnelle 	 * stored.
181df29a744SSven Schnelle 	 */
182df29a744SSven Schnelle 	p->restart_block.arch_data = 1;
1831da177e4SLinus Torvalds 	return 0;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
execve_tail(void)18656e62a73SSven Schnelle void execve_tail(void)
1871da177e4SLinus Torvalds {
18887c5c700SHeiko Carstens 	current->thread.ufpu.fpc = 0;
189f4e3de75SHeiko Carstens 	fpu_sfpc(0);
1901da177e4SLinus Torvalds }
19103ff9a23SMartin Schwidefsky 
__switch_to(struct task_struct * prev,struct task_struct * next)192340750c1SHeiko Carstens struct task_struct *__switch_to(struct task_struct *prev, struct task_struct *next)
193340750c1SHeiko Carstens {
19487c5c700SHeiko Carstens 	save_user_fpu_regs();
1954eed43deSHeiko Carstens 	save_kernel_fpu_regs(&prev->thread);
196340750c1SHeiko Carstens 	save_access_regs(&prev->thread.acrs[0]);
197340750c1SHeiko Carstens 	save_ri_cb(prev->thread.ri_cb);
198340750c1SHeiko Carstens 	save_gs_cb(prev->thread.gs_cb);
199340750c1SHeiko Carstens 	update_cr_regs(next);
2004eed43deSHeiko Carstens 	restore_kernel_fpu_regs(&next->thread);
201340750c1SHeiko Carstens 	restore_access_regs(&next->thread.acrs[0]);
202340750c1SHeiko Carstens 	restore_ri_cb(next->thread.ri_cb, prev->thread.ri_cb);
203340750c1SHeiko Carstens 	restore_gs_cb(next->thread.gs_cb);
204340750c1SHeiko Carstens 	return __switch_to_asm(prev, next);
205340750c1SHeiko Carstens }
206340750c1SHeiko Carstens 
__get_wchan(struct task_struct * p)20742a20f86SKees Cook unsigned long __get_wchan(struct task_struct *p)
2081da177e4SLinus Torvalds {
2096756dd9bSVasily Gorbik 	struct unwind_state state;
2106756dd9bSVasily Gorbik 	unsigned long ip = 0;
2111da177e4SLinus Torvalds 
21242a20f86SKees Cook 	if (!task_stack_page(p))
2131da177e4SLinus Torvalds 		return 0;
2148769f610SVasily Gorbik 
2158769f610SVasily Gorbik 	if (!try_get_task_stack(p))
2168769f610SVasily Gorbik 		return 0;
2178769f610SVasily Gorbik 
2186756dd9bSVasily Gorbik 	unwind_for_each_frame(&state, p, NULL, 0) {
2196756dd9bSVasily Gorbik 		if (state.stack_info.type != STACK_TYPE_TASK) {
2206756dd9bSVasily Gorbik 			ip = 0;
2216756dd9bSVasily Gorbik 			break;
2228769f610SVasily Gorbik 		}
2236756dd9bSVasily Gorbik 
2246756dd9bSVasily Gorbik 		ip = unwind_get_return_address(&state);
2256756dd9bSVasily Gorbik 		if (!ip)
2266756dd9bSVasily Gorbik 			break;
2276756dd9bSVasily Gorbik 
2286756dd9bSVasily Gorbik 		if (!in_sched_functions(ip))
2296756dd9bSVasily Gorbik 			break;
2308769f610SVasily Gorbik 	}
2316756dd9bSVasily Gorbik 
2328769f610SVasily Gorbik 	put_task_stack(p);
2336756dd9bSVasily Gorbik 	return ip;
2341da177e4SLinus Torvalds }
2359887a1fcSHeiko Carstens 
arch_align_stack(unsigned long sp)2369887a1fcSHeiko Carstens unsigned long arch_align_stack(unsigned long sp)
2379887a1fcSHeiko Carstens {
2389887a1fcSHeiko Carstens 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
2398032bf12SJason A. Donenfeld 		sp -= get_random_u32_below(PAGE_SIZE);
2409887a1fcSHeiko Carstens 	return sp & ~0xf;
2419887a1fcSHeiko Carstens }
24233519182SHeiko Carstens 
brk_rnd(void)24333519182SHeiko Carstens static inline unsigned long brk_rnd(void)
24433519182SHeiko Carstens {
245f743f16cSJason A. Donenfeld 	return (get_random_u16() & BRK_RND_MASK) << PAGE_SHIFT;
24633519182SHeiko Carstens }
24733519182SHeiko Carstens 
arch_randomize_brk(struct mm_struct * mm)24833519182SHeiko Carstens unsigned long arch_randomize_brk(struct mm_struct *mm)
24933519182SHeiko Carstens {
2509efe4f29SMartin Schwidefsky 	unsigned long ret;
25133519182SHeiko Carstens 
2529efe4f29SMartin Schwidefsky 	ret = PAGE_ALIGN(mm->brk + brk_rnd());
2539efe4f29SMartin Schwidefsky 	return (ret > mm->brk) ? ret : mm->brk;
25433519182SHeiko Carstens }
255