• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <asm/cpu_mf.h>
33 #include <asm/io.h>
34 #include <asm/processor.h>
35 #include <asm/vtimer.h>
36 #include <asm/exec.h>
37 #include <asm/irq.h>
38 #include <asm/nmi.h>
39 #include <asm/smp.h>
40 #include <asm/stacktrace.h>
41 #include <asm/switch_to.h>
42 #include <asm/runtime_instr.h>
43 #include <asm/unwind.h>
44 #include "entry.h"
45 
46 asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
47 
48 extern void kernel_thread_starter(void);
49 
flush_thread(void)50 void flush_thread(void)
51 {
52 }
53 
arch_setup_new_exec(void)54 void arch_setup_new_exec(void)
55 {
56 	if (S390_lowcore.current_pid != current->pid) {
57 		S390_lowcore.current_pid = current->pid;
58 		if (test_facility(40))
59 			lpp(&S390_lowcore.lpp);
60 	}
61 }
62 
arch_release_task_struct(struct task_struct * tsk)63 void arch_release_task_struct(struct task_struct *tsk)
64 {
65 	runtime_instr_release(tsk);
66 	guarded_storage_release(tsk);
67 }
68 
arch_dup_task_struct(struct task_struct * dst,struct task_struct * src)69 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
70 {
71 	/*
72 	 * Save the floating-point or vector register state of the current
73 	 * task and set the CIF_FPU flag to lazy restore the FPU register
74 	 * state when returning to user space.
75 	 */
76 	save_fpu_regs();
77 
78 	memcpy(dst, src, arch_task_struct_size);
79 	dst->thread.fpu.regs = dst->thread.fpu.fprs;
80 
81 	/*
82 	 * Don't transfer over the runtime instrumentation or the guarded
83 	 * storage control block pointers. These fields are cleared here instead
84 	 * of in copy_thread() to avoid premature freeing of associated memory
85 	 * on fork() failure. Wait to clear the RI flag because ->stack still
86 	 * refers to the source thread.
87 	 */
88 	dst->thread.ri_cb = NULL;
89 	dst->thread.gs_cb = NULL;
90 	dst->thread.gs_bc_cb = NULL;
91 
92 	return 0;
93 }
94 
copy_thread(unsigned long clone_flags,unsigned long new_stackp,unsigned long arg,struct task_struct * p,unsigned long tls)95 int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
96 		unsigned long arg, struct task_struct *p, unsigned long tls)
97 {
98 	struct fake_frame
99 	{
100 		struct stack_frame sf;
101 		struct pt_regs childregs;
102 	} *frame;
103 
104 	frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
105 	p->thread.ksp = (unsigned long) frame;
106 	/* Save access registers to new thread structure. */
107 	save_access_regs(&p->thread.acrs[0]);
108 	/* start new process with ar4 pointing to the correct address space */
109 	p->thread.mm_segment = get_fs();
110 	/* Don't copy debug registers */
111 	memset(&p->thread.per_user, 0, sizeof(p->thread.per_user));
112 	memset(&p->thread.per_event, 0, sizeof(p->thread.per_event));
113 	clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
114 	p->thread.per_flags = 0;
115 	/* Initialize per thread user and system timer values */
116 	p->thread.user_timer = 0;
117 	p->thread.guest_timer = 0;
118 	p->thread.system_timer = 0;
119 	p->thread.hardirq_timer = 0;
120 	p->thread.softirq_timer = 0;
121 	p->thread.last_break = 1;
122 
123 	frame->sf.back_chain = 0;
124 	/* new return point is ret_from_fork */
125 	frame->sf.gprs[8] = (unsigned long) ret_from_fork;
126 	/* fake return stack for resume(), don't go back to schedule */
127 	frame->sf.gprs[9] = (unsigned long) frame;
128 
129 	/* Store access registers to kernel stack of new process. */
130 	if (unlikely(p->flags & (PF_KTHREAD | PF_IO_WORKER))) {
131 		/* kernel thread */
132 		memset(&frame->childregs, 0, sizeof(struct pt_regs));
133 		frame->childregs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_DAT |
134 				PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
135 		frame->childregs.psw.addr =
136 				(unsigned long) kernel_thread_starter;
137 		frame->childregs.gprs[9] = new_stackp; /* function */
138 		frame->childregs.gprs[10] = arg;
139 		frame->childregs.gprs[11] = (unsigned long) do_exit;
140 		frame->childregs.orig_gpr2 = -1;
141 
142 		return 0;
143 	}
144 	frame->childregs = *current_pt_regs();
145 	frame->childregs.gprs[2] = 0;	/* child returns 0 on fork. */
146 	frame->childregs.flags = 0;
147 	if (new_stackp)
148 		frame->childregs.gprs[15] = new_stackp;
149 	/*
150 	 * Clear the runtime instrumentation flag after the above childregs
151 	 * copy. The CB pointer was already cleared in arch_dup_task_struct().
152 	 */
153 	frame->childregs.psw.mask &= ~PSW_MASK_RI;
154 
155 	/* Set a new TLS ?  */
156 	if (clone_flags & CLONE_SETTLS) {
157 		if (is_compat_task()) {
158 			p->thread.acrs[0] = (unsigned int)tls;
159 		} else {
160 			p->thread.acrs[0] = (unsigned int)(tls >> 32);
161 			p->thread.acrs[1] = (unsigned int)tls;
162 		}
163 	}
164 	return 0;
165 }
166 
execve_tail(void)167 asmlinkage void execve_tail(void)
168 {
169 	current->thread.fpu.fpc = 0;
170 	asm volatile("sfpc %0" : : "d" (0));
171 }
172 
get_wchan(struct task_struct * p)173 unsigned long get_wchan(struct task_struct *p)
174 {
175 	struct unwind_state state;
176 	unsigned long ip = 0;
177 
178 	if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
179 		return 0;
180 
181 	if (!try_get_task_stack(p))
182 		return 0;
183 
184 	unwind_for_each_frame(&state, p, NULL, 0) {
185 		if (state.stack_info.type != STACK_TYPE_TASK) {
186 			ip = 0;
187 			break;
188 		}
189 
190 		ip = unwind_get_return_address(&state);
191 		if (!ip)
192 			break;
193 
194 		if (!in_sched_functions(ip))
195 			break;
196 	}
197 
198 	put_task_stack(p);
199 	return ip;
200 }
201 
arch_align_stack(unsigned long sp)202 unsigned long arch_align_stack(unsigned long sp)
203 {
204 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
205 		sp -= get_random_int() & ~PAGE_MASK;
206 	return sp & ~0xf;
207 }
208 
brk_rnd(void)209 static inline unsigned long brk_rnd(void)
210 {
211 	return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT;
212 }
213 
arch_randomize_brk(struct mm_struct * mm)214 unsigned long arch_randomize_brk(struct mm_struct *mm)
215 {
216 	unsigned long ret;
217 
218 	ret = PAGE_ALIGN(mm->brk + brk_rnd());
219 	return (ret > mm->brk) ? ret : mm->brk;
220 }
221 
set_fs_fixup(void)222 void set_fs_fixup(void)
223 {
224 	struct pt_regs *regs = current_pt_regs();
225 	static bool warned;
226 
227 	set_fs(USER_DS);
228 	if (warned)
229 		return;
230 	WARN(1, "Unbalanced set_fs - int code: 0x%x\n", regs->int_code);
231 	show_registers(regs);
232 	warned = true;
233 }
234