xref: /openbmc/linux/arch/s390/kernel/process.c (revision 4b2a108c)
1 /*
2  * This file handles the architecture dependent parts of process handling.
3  *
4  *    Copyright IBM Corp. 1999,2009
5  *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
6  *		 Hartmut Penner <hp@de.ibm.com>,
7  *		 Denis Joseph Barrow,
8  */
9 
10 #include <linux/compiler.h>
11 #include <linux/cpu.h>
12 #include <linux/errno.h>
13 #include <linux/sched.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/fs.h>
17 #include <linux/smp.h>
18 #include <linux/stddef.h>
19 #include <linux/unistd.h>
20 #include <linux/ptrace.h>
21 #include <linux/slab.h>
22 #include <linux/vmalloc.h>
23 #include <linux/user.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/reboot.h>
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/notifier.h>
30 #include <linux/utsname.h>
31 #include <linux/tick.h>
32 #include <linux/elfcore.h>
33 #include <linux/kernel_stat.h>
34 #include <linux/syscalls.h>
35 #include <asm/compat.h>
36 #include <asm/uaccess.h>
37 #include <asm/pgtable.h>
38 #include <asm/system.h>
39 #include <asm/io.h>
40 #include <asm/processor.h>
41 #include <asm/irq.h>
42 #include <asm/timer.h>
43 #include <asm/nmi.h>
44 #include "entry.h"
45 
46 asmlinkage void ret_from_fork(void) asm ("ret_from_fork");
47 
48 /*
49  * Return saved PC of a blocked thread. used in kernel/sched.
50  * resume in entry.S does not create a new stack frame, it
51  * just stores the registers %r6-%r15 to the frame given by
52  * schedule. We want to return the address of the caller of
53  * schedule, so we have to walk the backchain one time to
54  * find the frame schedule() store its return address.
55  */
56 unsigned long thread_saved_pc(struct task_struct *tsk)
57 {
58 	struct stack_frame *sf, *low, *high;
59 
60 	if (!tsk || !task_stack_page(tsk))
61 		return 0;
62 	low = task_stack_page(tsk);
63 	high = (struct stack_frame *) task_pt_regs(tsk);
64 	sf = (struct stack_frame *) (tsk->thread.ksp & PSW_ADDR_INSN);
65 	if (sf <= low || sf > high)
66 		return 0;
67 	sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
68 	if (sf <= low || sf > high)
69 		return 0;
70 	return sf->gprs[8];
71 }
72 
73 /*
74  * The idle loop on a S390...
75  */
76 static void default_idle(void)
77 {
78 	/* CPU is going idle. */
79 	local_irq_disable();
80 	if (need_resched()) {
81 		local_irq_enable();
82 		return;
83 	}
84 #ifdef CONFIG_HOTPLUG_CPU
85 	if (cpu_is_offline(smp_processor_id())) {
86 		preempt_enable_no_resched();
87 		cpu_die();
88 	}
89 #endif
90 	local_mcck_disable();
91 	if (test_thread_flag(TIF_MCCK_PENDING)) {
92 		local_mcck_enable();
93 		local_irq_enable();
94 		s390_handle_mcck();
95 		return;
96 	}
97 	trace_hardirqs_on();
98 	/* Don't trace preempt off for idle. */
99 	stop_critical_timings();
100 	/* Stop virtual timer and halt the cpu. */
101 	vtime_stop_cpu();
102 	/* Reenable preemption tracer. */
103 	start_critical_timings();
104 }
105 
106 void cpu_idle(void)
107 {
108 	for (;;) {
109 		tick_nohz_stop_sched_tick(1);
110 		while (!need_resched())
111 			default_idle();
112 		tick_nohz_restart_sched_tick();
113 		preempt_enable_no_resched();
114 		schedule();
115 		preempt_disable();
116 	}
117 }
118 
119 extern void kernel_thread_starter(void);
120 
121 asm(
122 	".align 4\n"
123 	"kernel_thread_starter:\n"
124 	"    la    2,0(10)\n"
125 	"    basr  14,9\n"
126 	"    la    2,0\n"
127 	"    br    11\n");
128 
129 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
130 {
131 	struct pt_regs regs;
132 
133 	memset(&regs, 0, sizeof(regs));
134 	regs.psw.mask = psw_kernel_bits | PSW_MASK_IO | PSW_MASK_EXT;
135 	regs.psw.addr = (unsigned long) kernel_thread_starter | PSW_ADDR_AMODE;
136 	regs.gprs[9] = (unsigned long) fn;
137 	regs.gprs[10] = (unsigned long) arg;
138 	regs.gprs[11] = (unsigned long) do_exit;
139 	regs.orig_gpr2 = -1;
140 
141 	/* Ok, create the new process.. */
142 	return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
143 		       0, &regs, 0, NULL, NULL);
144 }
145 EXPORT_SYMBOL(kernel_thread);
146 
147 /*
148  * Free current thread data structures etc..
149  */
150 void exit_thread(void)
151 {
152 }
153 
154 void flush_thread(void)
155 {
156 	clear_used_math();
157 	clear_tsk_thread_flag(current, TIF_USEDFPU);
158 }
159 
160 void release_thread(struct task_struct *dead_task)
161 {
162 }
163 
164 int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
165 		unsigned long unused,
166 		struct task_struct *p, struct pt_regs *regs)
167 {
168 	struct thread_info *ti;
169 	struct fake_frame
170 	{
171 		struct stack_frame sf;
172 		struct pt_regs childregs;
173 	} *frame;
174 
175 	frame = container_of(task_pt_regs(p), struct fake_frame, childregs);
176 	p->thread.ksp = (unsigned long) frame;
177 	/* Store access registers to kernel stack of new process. */
178 	frame->childregs = *regs;
179 	frame->childregs.gprs[2] = 0;	/* child returns 0 on fork. */
180 	frame->childregs.gprs[15] = new_stackp;
181 	frame->sf.back_chain = 0;
182 
183 	/* new return point is ret_from_fork */
184 	frame->sf.gprs[8] = (unsigned long) ret_from_fork;
185 
186 	/* fake return stack for resume(), don't go back to schedule */
187 	frame->sf.gprs[9] = (unsigned long) frame;
188 
189 	/* Save access registers to new thread structure. */
190 	save_access_regs(&p->thread.acrs[0]);
191 
192 #ifndef CONFIG_64BIT
193 	/*
194 	 * save fprs to current->thread.fp_regs to merge them with
195 	 * the emulated registers and then copy the result to the child.
196 	 */
197 	save_fp_regs(&current->thread.fp_regs);
198 	memcpy(&p->thread.fp_regs, &current->thread.fp_regs,
199 	       sizeof(s390_fp_regs));
200 	/* Set a new TLS ?  */
201 	if (clone_flags & CLONE_SETTLS)
202 		p->thread.acrs[0] = regs->gprs[6];
203 #else /* CONFIG_64BIT */
204 	/* Save the fpu registers to new thread structure. */
205 	save_fp_regs(&p->thread.fp_regs);
206 	/* Set a new TLS ?  */
207 	if (clone_flags & CLONE_SETTLS) {
208 		if (is_compat_task()) {
209 			p->thread.acrs[0] = (unsigned int) regs->gprs[6];
210 		} else {
211 			p->thread.acrs[0] = (unsigned int)(regs->gprs[6] >> 32);
212 			p->thread.acrs[1] = (unsigned int) regs->gprs[6];
213 		}
214 	}
215 #endif /* CONFIG_64BIT */
216 	/* start new process with ar4 pointing to the correct address space */
217 	p->thread.mm_segment = get_fs();
218 	/* Don't copy debug registers */
219 	memset(&p->thread.per_info, 0, sizeof(p->thread.per_info));
220 	/* Initialize per thread user and system timer values */
221 	ti = task_thread_info(p);
222 	ti->user_timer = 0;
223 	ti->system_timer = 0;
224 	return 0;
225 }
226 
227 SYSCALL_DEFINE0(fork)
228 {
229 	struct pt_regs *regs = task_pt_regs(current);
230 	return do_fork(SIGCHLD, regs->gprs[15], regs, 0, NULL, NULL);
231 }
232 
233 SYSCALL_DEFINE0(clone)
234 {
235 	struct pt_regs *regs = task_pt_regs(current);
236 	unsigned long clone_flags;
237 	unsigned long newsp;
238 	int __user *parent_tidptr, *child_tidptr;
239 
240 	clone_flags = regs->gprs[3];
241 	newsp = regs->orig_gpr2;
242 	parent_tidptr = (int __user *) regs->gprs[4];
243 	child_tidptr = (int __user *) regs->gprs[5];
244 	if (!newsp)
245 		newsp = regs->gprs[15];
246 	return do_fork(clone_flags, newsp, regs, 0,
247 		       parent_tidptr, child_tidptr);
248 }
249 
250 /*
251  * This is trivial, and on the face of it looks like it
252  * could equally well be done in user mode.
253  *
254  * Not so, for quite unobvious reasons - register pressure.
255  * In user mode vfork() cannot have a stack frame, and if
256  * done by calling the "clone()" system call directly, you
257  * do not have enough call-clobbered registers to hold all
258  * the information you need.
259  */
260 SYSCALL_DEFINE0(vfork)
261 {
262 	struct pt_regs *regs = task_pt_regs(current);
263 	return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
264 		       regs->gprs[15], regs, 0, NULL, NULL);
265 }
266 
267 asmlinkage void execve_tail(void)
268 {
269 	current->thread.fp_regs.fpc = 0;
270 	if (MACHINE_HAS_IEEE)
271 		asm volatile("sfpc %0,%0" : : "d" (0));
272 }
273 
274 /*
275  * sys_execve() executes a new program.
276  */
277 SYSCALL_DEFINE0(execve)
278 {
279 	struct pt_regs *regs = task_pt_regs(current);
280 	char *filename;
281 	unsigned long result;
282 	int rc;
283 
284 	filename = getname((char __user *) regs->orig_gpr2);
285 	if (IS_ERR(filename)) {
286 		result = PTR_ERR(filename);
287 		goto out;
288 	}
289 	rc = do_execve(filename, (char __user * __user *) regs->gprs[3],
290 		       (char __user * __user *) regs->gprs[4], regs);
291 	if (rc) {
292 		result = rc;
293 		goto out_putname;
294 	}
295 	execve_tail();
296 	result = regs->gprs[2];
297 out_putname:
298 	putname(filename);
299 out:
300 	return result;
301 }
302 
303 /*
304  * fill in the FPU structure for a core dump.
305  */
306 int dump_fpu (struct pt_regs * regs, s390_fp_regs *fpregs)
307 {
308 #ifndef CONFIG_64BIT
309 	/*
310 	 * save fprs to current->thread.fp_regs to merge them with
311 	 * the emulated registers and then copy the result to the dump.
312 	 */
313 	save_fp_regs(&current->thread.fp_regs);
314 	memcpy(fpregs, &current->thread.fp_regs, sizeof(s390_fp_regs));
315 #else /* CONFIG_64BIT */
316 	save_fp_regs(fpregs);
317 #endif /* CONFIG_64BIT */
318 	return 1;
319 }
320 EXPORT_SYMBOL(dump_fpu);
321 
322 unsigned long get_wchan(struct task_struct *p)
323 {
324 	struct stack_frame *sf, *low, *high;
325 	unsigned long return_address;
326 	int count;
327 
328 	if (!p || p == current || p->state == TASK_RUNNING || !task_stack_page(p))
329 		return 0;
330 	low = task_stack_page(p);
331 	high = (struct stack_frame *) task_pt_regs(p);
332 	sf = (struct stack_frame *) (p->thread.ksp & PSW_ADDR_INSN);
333 	if (sf <= low || sf > high)
334 		return 0;
335 	for (count = 0; count < 16; count++) {
336 		sf = (struct stack_frame *) (sf->back_chain & PSW_ADDR_INSN);
337 		if (sf <= low || sf > high)
338 			return 0;
339 		return_address = sf->gprs[8] & PSW_ADDR_INSN;
340 		if (!in_sched_functions(return_address))
341 			return return_address;
342 	}
343 	return 0;
344 }
345