xref: /linux-6.15/arch/s390/kernel/process.c (revision 56e62a73)
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>
32*56e62a73SSven 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 
47*56e62a73SSven Schnelle void ret_from_fork(void) asm("ret_from_fork");
481da177e4SLinus Torvalds 
49*56e62a73SSven Schnelle void __ret_from_fork(struct task_struct *prev, struct pt_regs *regs)
50*56e62a73SSven Schnelle {
51*56e62a73SSven Schnelle 	void (*func)(void *arg);
52*56e62a73SSven Schnelle 
53*56e62a73SSven Schnelle 	schedule_tail(prev);
54*56e62a73SSven Schnelle 
55*56e62a73SSven Schnelle 	if (!user_mode(regs)) {
56*56e62a73SSven Schnelle 		/* Kernel thread */
57*56e62a73SSven Schnelle 		func = (void *)regs->gprs[9];
58*56e62a73SSven Schnelle 		func((void *)regs->gprs[10]);
59*56e62a73SSven Schnelle 	}
60*56e62a73SSven Schnelle 	clear_pt_regs_flag(regs, PIF_SYSCALL);
61*56e62a73SSven Schnelle 	syscall_exit_to_user_mode(regs);
62*56e62a73SSven 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;
94155e839aSHendrik Brueckner 	return 0;
956a039eabSHendrik Brueckner }
966a039eabSHendrik Brueckner 
97714acdbdSChristian Brauner int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
98f50c0e63SHeiko Carstens 		unsigned long arg, struct task_struct *p, unsigned long tls)
991da177e4SLinus Torvalds {
1001da177e4SLinus Torvalds 	struct fake_frame
1011da177e4SLinus Torvalds 	{
1021da177e4SLinus Torvalds 		struct stack_frame sf;
1031da177e4SLinus Torvalds 		struct pt_regs childregs;
1041da177e4SLinus Torvalds 	} *frame;
1051da177e4SLinus Torvalds 
106c7584fb6SAl Viro 	frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
1071da177e4SLinus Torvalds 	p->thread.ksp = (unsigned long) frame;
108f9a7e025SAl Viro 	/* Save access registers to new thread structure. */
109f9a7e025SAl Viro 	save_access_regs(&p->thread.acrs[0]);
110f9a7e025SAl Viro 	/* start new process with ar4 pointing to the correct address space */
111f9a7e025SAl Viro 	/* Don't copy debug registers */
112f9a7e025SAl Viro 	memset(&p->thread.per_user, 0, sizeof(p->thread.per_user));
113f9a7e025SAl Viro 	memset(&p->thread.per_event, 0, sizeof(p->thread.per_event));
114f9a7e025SAl Viro 	clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
115a1c5befcSHeiko Carstens 	p->thread.per_flags = 0;
116f9a7e025SAl Viro 	/* Initialize per thread user and system timer values */
11790c53e65SMartin Schwidefsky 	p->thread.user_timer = 0;
1183c915bdcSMartin Schwidefsky 	p->thread.guest_timer = 0;
11990c53e65SMartin Schwidefsky 	p->thread.system_timer = 0;
1203c915bdcSMartin Schwidefsky 	p->thread.hardirq_timer = 0;
1213c915bdcSMartin Schwidefsky 	p->thread.softirq_timer = 0;
1220b38b5e1SSven Schnelle 	p->thread.last_break = 1;
1231da177e4SLinus Torvalds 
124f9a7e025SAl Viro 	frame->sf.back_chain = 0;
125*56e62a73SSven Schnelle 	frame->sf.gprs[5] = (unsigned long)frame + sizeof(struct stack_frame);
126*56e62a73SSven Schnelle 	frame->sf.gprs[6] = (unsigned long)p;
1271da177e4SLinus Torvalds 	/* new return point is ret_from_fork */
1281da177e4SLinus Torvalds 	frame->sf.gprs[8] = (unsigned long)ret_from_fork;
1291da177e4SLinus Torvalds 	/* fake return stack for resume(), don't go back to schedule */
1301da177e4SLinus Torvalds 	frame->sf.gprs[9] = (unsigned long)frame;
1311da177e4SLinus Torvalds 
132f9a7e025SAl Viro 	/* Store access registers to kernel stack of new process. */
13387f1ca8fSAl Viro 	if (unlikely(p->flags & PF_KTHREAD)) {
134f9a7e025SAl Viro 		/* kernel thread */
135f9a7e025SAl Viro 		memset(&frame->childregs, 0, sizeof(struct pt_regs));
136e258d719SMartin Schwidefsky 		frame->childregs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_DAT |
137f9a7e025SAl Viro 				PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
138fecc868aSHeiko Carstens 		frame->childregs.psw.addr =
139*56e62a73SSven Schnelle 				(unsigned long)__ret_from_fork;
140f9a7e025SAl Viro 		frame->childregs.gprs[9] = new_stackp; /* function */
141f9a7e025SAl Viro 		frame->childregs.gprs[10] = arg;
142f9a7e025SAl Viro 		frame->childregs.gprs[11] = (unsigned long)do_exit;
143f9a7e025SAl Viro 		frame->childregs.orig_gpr2 = -1;
144f9a7e025SAl Viro 
145f9a7e025SAl Viro 		return 0;
146f9a7e025SAl Viro 	}
14787f1ca8fSAl Viro 	frame->childregs = *current_pt_regs();
148f9a7e025SAl Viro 	frame->childregs.gprs[2] = 0;	/* child returns 0 on fork. */
149d3a73acbSMartin Schwidefsky 	frame->childregs.flags = 0;
15087f1ca8fSAl Viro 	if (new_stackp)
151f9a7e025SAl Viro 		frame->childregs.gprs[15] = new_stackp;
1521da177e4SLinus Torvalds 
153e4b8b3f3SJan Glauber 	/* Don't copy runtime instrumentation info */
154e4b8b3f3SJan Glauber 	p->thread.ri_cb = NULL;
155e4b8b3f3SJan Glauber 	frame->childregs.psw.mask &= ~PSW_MASK_RI;
156916cda1aSMartin Schwidefsky 	/* Don't copy guarded storage control block */
157916cda1aSMartin Schwidefsky 	p->thread.gs_cb = NULL;
158916cda1aSMartin Schwidefsky 	p->thread.gs_bc_cb = NULL;
159e4b8b3f3SJan Glauber 
1601da177e4SLinus Torvalds 	/* Set a new TLS ?  */
1611da177e4SLinus Torvalds 	if (clone_flags & CLONE_SETTLS) {
1627757591aSHeiko Carstens 		if (is_compat_task()) {
16387f1ca8fSAl Viro 			p->thread.acrs[0] = (unsigned int)tls;
1641da177e4SLinus Torvalds 		} else {
16587f1ca8fSAl Viro 			p->thread.acrs[0] = (unsigned int)(tls >> 32);
16687f1ca8fSAl Viro 			p->thread.acrs[1] = (unsigned int)tls;
1671da177e4SLinus Torvalds 		}
1681da177e4SLinus Torvalds 	}
1691da177e4SLinus Torvalds 	return 0;
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
172*56e62a73SSven Schnelle void execve_tail(void)
1731da177e4SLinus Torvalds {
174904818e2SHendrik Brueckner 	current->thread.fpu.fpc = 0;
175e47994ddSHeiko Carstens 	asm volatile("sfpc %0" : : "d" (0));
1761da177e4SLinus Torvalds }
17703ff9a23SMartin Schwidefsky 
1781da177e4SLinus Torvalds unsigned long get_wchan(struct task_struct *p)
1791da177e4SLinus Torvalds {
1806756dd9bSVasily Gorbik 	struct unwind_state state;
1816756dd9bSVasily Gorbik 	unsigned long ip = 0;
1821da177e4SLinus Torvalds 
18330af7120SAl Viro 	if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
1841da177e4SLinus Torvalds 		return 0;
1858769f610SVasily Gorbik 
1868769f610SVasily Gorbik 	if (!try_get_task_stack(p))
1878769f610SVasily Gorbik 		return 0;
1888769f610SVasily Gorbik 
1896756dd9bSVasily Gorbik 	unwind_for_each_frame(&state, p, NULL, 0) {
1906756dd9bSVasily Gorbik 		if (state.stack_info.type != STACK_TYPE_TASK) {
1916756dd9bSVasily Gorbik 			ip = 0;
1926756dd9bSVasily Gorbik 			break;
1938769f610SVasily Gorbik 		}
1946756dd9bSVasily Gorbik 
1956756dd9bSVasily Gorbik 		ip = unwind_get_return_address(&state);
1966756dd9bSVasily Gorbik 		if (!ip)
1976756dd9bSVasily Gorbik 			break;
1986756dd9bSVasily Gorbik 
1996756dd9bSVasily Gorbik 		if (!in_sched_functions(ip))
2006756dd9bSVasily Gorbik 			break;
2018769f610SVasily Gorbik 	}
2026756dd9bSVasily Gorbik 
2038769f610SVasily Gorbik 	put_task_stack(p);
2046756dd9bSVasily Gorbik 	return ip;
2051da177e4SLinus Torvalds }
2069887a1fcSHeiko Carstens 
2079887a1fcSHeiko Carstens unsigned long arch_align_stack(unsigned long sp)
2089887a1fcSHeiko Carstens {
2099887a1fcSHeiko Carstens 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
2109887a1fcSHeiko Carstens 		sp -= get_random_int() & ~PAGE_MASK;
2119887a1fcSHeiko Carstens 	return sp & ~0xf;
2129887a1fcSHeiko Carstens }
21333519182SHeiko Carstens 
21433519182SHeiko Carstens static inline unsigned long brk_rnd(void)
21533519182SHeiko Carstens {
216c7e8b2c2SMartin Schwidefsky 	return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT;
21733519182SHeiko Carstens }
21833519182SHeiko Carstens 
21933519182SHeiko Carstens unsigned long arch_randomize_brk(struct mm_struct *mm)
22033519182SHeiko Carstens {
2219efe4f29SMartin Schwidefsky 	unsigned long ret;
22233519182SHeiko Carstens 
2239efe4f29SMartin Schwidefsky 	ret = PAGE_ALIGN(mm->brk + brk_rnd());
2249efe4f29SMartin Schwidefsky 	return (ret > mm->brk) ? ret : mm->brk;
22533519182SHeiko Carstens }
226