Linux 6.10-rc3
[sfrench/cifs-2.6.git] / arch / s390 / kernel / process.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This file handles the architecture dependent parts of process handling.
4  *
5  *    Copyright IBM Corp. 1999, 2009
6  *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
7  *               Hartmut Penner <hp@de.ibm.com>,
8  *               Denis Joseph Barrow,
9  */
10
11 #include <linux/elf-randomize.h>
12 #include <linux/compiler.h>
13 #include <linux/cpu.h>
14 #include <linux/sched.h>
15 #include <linux/sched/debug.h>
16 #include <linux/sched/task.h>
17 #include <linux/sched/task_stack.h>
18 #include <linux/kernel.h>
19 #include <linux/mm.h>
20 #include <linux/elfcore.h>
21 #include <linux/smp.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/tick.h>
25 #include <linux/personality.h>
26 #include <linux/syscalls.h>
27 #include <linux/compat.h>
28 #include <linux/kprobes.h>
29 #include <linux/random.h>
30 #include <linux/export.h>
31 #include <linux/init_task.h>
32 #include <linux/entry-common.h>
33 #include <linux/io.h>
34 #include <asm/guarded_storage.h>
35 #include <asm/access-regs.h>
36 #include <asm/switch_to.h>
37 #include <asm/cpu_mf.h>
38 #include <asm/processor.h>
39 #include <asm/ptrace.h>
40 #include <asm/vtimer.h>
41 #include <asm/exec.h>
42 #include <asm/fpu.h>
43 #include <asm/irq.h>
44 #include <asm/nmi.h>
45 #include <asm/smp.h>
46 #include <asm/stacktrace.h>
47 #include <asm/runtime_instr.h>
48 #include <asm/unwind.h>
49 #include "entry.h"
50
51 void ret_from_fork(void) asm("ret_from_fork");
52
53 void __ret_from_fork(struct task_struct *prev, struct pt_regs *regs)
54 {
55         void (*func)(void *arg);
56
57         schedule_tail(prev);
58
59         if (!user_mode(regs)) {
60                 /* Kernel thread */
61                 func = (void *)regs->gprs[9];
62                 func((void *)regs->gprs[10]);
63         }
64         clear_pt_regs_flag(regs, PIF_SYSCALL);
65         syscall_exit_to_user_mode(regs);
66 }
67
68 void flush_thread(void)
69 {
70 }
71
72 void arch_setup_new_exec(void)
73 {
74         if (S390_lowcore.current_pid != current->pid) {
75                 S390_lowcore.current_pid = current->pid;
76                 if (test_facility(40))
77                         lpp(&S390_lowcore.lpp);
78         }
79 }
80
81 void arch_release_task_struct(struct task_struct *tsk)
82 {
83         runtime_instr_release(tsk);
84         guarded_storage_release(tsk);
85 }
86
87 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
88 {
89         save_user_fpu_regs();
90
91         *dst = *src;
92         dst->thread.kfpu_flags = 0;
93
94         /*
95          * Don't transfer over the runtime instrumentation or the guarded
96          * storage control block pointers. These fields are cleared here instead
97          * of in copy_thread() to avoid premature freeing of associated memory
98          * on fork() failure. Wait to clear the RI flag because ->stack still
99          * refers to the source thread.
100          */
101         dst->thread.ri_cb = NULL;
102         dst->thread.gs_cb = NULL;
103         dst->thread.gs_bc_cb = NULL;
104
105         return 0;
106 }
107
108 int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
109 {
110         unsigned long clone_flags = args->flags;
111         unsigned long new_stackp = args->stack;
112         unsigned long tls = args->tls;
113         struct fake_frame
114         {
115                 struct stack_frame sf;
116                 struct pt_regs childregs;
117         } *frame;
118
119         frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
120         p->thread.ksp = (unsigned long) frame;
121         /* Save access registers to new thread structure. */
122         save_access_regs(&p->thread.acrs[0]);
123         /* start new process with ar4 pointing to the correct address space */
124         /* Don't copy debug registers */
125         memset(&p->thread.per_user, 0, sizeof(p->thread.per_user));
126         memset(&p->thread.per_event, 0, sizeof(p->thread.per_event));
127         clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
128         p->thread.per_flags = 0;
129         /* Initialize per thread user and system timer values */
130         p->thread.user_timer = 0;
131         p->thread.guest_timer = 0;
132         p->thread.system_timer = 0;
133         p->thread.hardirq_timer = 0;
134         p->thread.softirq_timer = 0;
135         p->thread.last_break = 1;
136
137         frame->sf.back_chain = 0;
138         frame->sf.gprs[11 - 6] = (unsigned long)&frame->childregs;
139         frame->sf.gprs[12 - 6] = (unsigned long)p;
140         /* new return point is ret_from_fork */
141         frame->sf.gprs[14 - 6] = (unsigned long)ret_from_fork;
142         /* fake return stack for resume(), don't go back to schedule */
143         frame->sf.gprs[15 - 6] = (unsigned long)frame;
144
145         /* Store access registers to kernel stack of new process. */
146         if (unlikely(args->fn)) {
147                 /* kernel thread */
148                 memset(&frame->childregs, 0, sizeof(struct pt_regs));
149                 frame->childregs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_IO |
150                                             PSW_MASK_EXT | PSW_MASK_MCHECK;
151                 frame->childregs.gprs[9] = (unsigned long)args->fn;
152                 frame->childregs.gprs[10] = (unsigned long)args->fn_arg;
153                 frame->childregs.orig_gpr2 = -1;
154                 frame->childregs.last_break = 1;
155                 return 0;
156         }
157         frame->childregs = *current_pt_regs();
158         frame->childregs.gprs[2] = 0;   /* child returns 0 on fork. */
159         frame->childregs.flags = 0;
160         if (new_stackp)
161                 frame->childregs.gprs[15] = new_stackp;
162         /*
163          * Clear the runtime instrumentation flag after the above childregs
164          * copy. The CB pointer was already cleared in arch_dup_task_struct().
165          */
166         frame->childregs.psw.mask &= ~PSW_MASK_RI;
167
168         /* Set a new TLS ?  */
169         if (clone_flags & CLONE_SETTLS) {
170                 if (is_compat_task()) {
171                         p->thread.acrs[0] = (unsigned int)tls;
172                 } else {
173                         p->thread.acrs[0] = (unsigned int)(tls >> 32);
174                         p->thread.acrs[1] = (unsigned int)tls;
175                 }
176         }
177         /*
178          * s390 stores the svc return address in arch_data when calling
179          * sigreturn()/restart_syscall() via vdso. 1 means no valid address
180          * stored.
181          */
182         p->restart_block.arch_data = 1;
183         return 0;
184 }
185
186 void execve_tail(void)
187 {
188         current->thread.ufpu.fpc = 0;
189         fpu_sfpc(0);
190 }
191
192 struct task_struct *__switch_to(struct task_struct *prev, struct task_struct *next)
193 {
194         save_user_fpu_regs();
195         save_kernel_fpu_regs(&prev->thread);
196         save_access_regs(&prev->thread.acrs[0]);
197         save_ri_cb(prev->thread.ri_cb);
198         save_gs_cb(prev->thread.gs_cb);
199         update_cr_regs(next);
200         restore_kernel_fpu_regs(&next->thread);
201         restore_access_regs(&next->thread.acrs[0]);
202         restore_ri_cb(next->thread.ri_cb, prev->thread.ri_cb);
203         restore_gs_cb(next->thread.gs_cb);
204         return __switch_to_asm(prev, next);
205 }
206
207 unsigned long __get_wchan(struct task_struct *p)
208 {
209         struct unwind_state state;
210         unsigned long ip = 0;
211
212         if (!task_stack_page(p))
213                 return 0;
214
215         if (!try_get_task_stack(p))
216                 return 0;
217
218         unwind_for_each_frame(&state, p, NULL, 0) {
219                 if (state.stack_info.type != STACK_TYPE_TASK) {
220                         ip = 0;
221                         break;
222                 }
223
224                 ip = unwind_get_return_address(&state);
225                 if (!ip)
226                         break;
227
228                 if (!in_sched_functions(ip))
229                         break;
230         }
231
232         put_task_stack(p);
233         return ip;
234 }
235
236 unsigned long arch_align_stack(unsigned long sp)
237 {
238         if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
239                 sp -= get_random_u32_below(PAGE_SIZE);
240         return sp & ~0xf;
241 }
242
243 static inline unsigned long brk_rnd(void)
244 {
245         return (get_random_u16() & BRK_RND_MASK) << PAGE_SHIFT;
246 }
247
248 unsigned long arch_randomize_brk(struct mm_struct *mm)
249 {
250         unsigned long ret;
251
252         ret = PAGE_ALIGN(mm->brk + brk_rnd());
253         return (ret > mm->brk) ? ret : mm->brk;
254 }