blob: 5d335d178706ae53966fbb01e3a039061820471a [file] [log] [blame]
Greg Ungererfde39442012-02-09 14:18:46 +10001/*
2 * linux/arch/m68k/kernel/process.c
3 *
4 * Copyright (C) 1995 Hamish Macdonald
5 *
6 * 68060 fixes by Jesper Skov
7 */
8
9/*
10 * This file handles the architecture-dependent parts of process handling..
11 */
12
13#include <linux/errno.h>
14#include <linux/module.h>
15#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010016#include <linux/sched/debug.h>
Ingo Molnar29930022017-02-08 18:51:36 +010017#include <linux/sched/task.h>
Greg Ungererfde39442012-02-09 14:18:46 +100018#include <linux/kernel.h>
19#include <linux/mm.h>
20#include <linux/slab.h>
21#include <linux/fs.h>
22#include <linux/smp.h>
23#include <linux/stddef.h>
24#include <linux/unistd.h>
25#include <linux/ptrace.h>
26#include <linux/user.h>
27#include <linux/reboot.h>
28#include <linux/init_task.h>
29#include <linux/mqueue.h>
Frederic Weisbecker5b57ba32012-08-22 17:27:34 +020030#include <linux/rcupdate.h>
Greg Ungererfde39442012-02-09 14:18:46 +100031
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080032#include <linux/uaccess.h>
Greg Ungererfde39442012-02-09 14:18:46 +100033#include <asm/traps.h>
34#include <asm/machdep.h>
35#include <asm/setup.h>
36#include <asm/pgtable.h>
37
38
39asmlinkage void ret_from_fork(void);
Al Viro533e6902012-09-16 12:05:09 -040040asmlinkage void ret_from_kernel_thread(void);
Greg Ungererfde39442012-02-09 14:18:46 +100041
42
43/*
44 * Return saved PC from a blocked thread
45 */
46unsigned long thread_saved_pc(struct task_struct *tsk)
47{
48 struct switch_stack *sw = (struct switch_stack *)tsk->thread.ksp;
49 /* Check whether the thread is blocked in resume() */
50 if (in_sched_functions(sw->retpc))
51 return ((unsigned long *)sw->a6)[1];
52 else
53 return sw->retpc;
54}
55
Thomas Gleixnerdfa174d2013-03-21 22:49:49 +010056void arch_cpu_idle(void)
Greg Ungererfde39442012-02-09 14:18:46 +100057{
Greg Ungererfde39442012-02-09 14:18:46 +100058#if defined(MACH_ATARI_ONLY)
Thomas Gleixnerdfa174d2013-03-21 22:49:49 +010059 /* block out HSYNC on the atari (falcon) */
60 __asm__("stop #0x2200" : : : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#else
Thomas Gleixnerdfa174d2013-03-21 22:49:49 +010062 __asm__("stop #0x2000" : : : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#endif
Greg Ungererfde39442012-02-09 14:18:46 +100064}
65
Greg Ungererfde39442012-02-09 14:18:46 +100066void machine_restart(char * __unused)
67{
68 if (mach_reset)
69 mach_reset();
70 for (;;);
71}
72
73void machine_halt(void)
74{
75 if (mach_halt)
76 mach_halt();
77 for (;;);
78}
79
80void machine_power_off(void)
81{
82 if (mach_power_off)
83 mach_power_off();
84 for (;;);
85}
86
87void (*pm_power_off)(void) = machine_power_off;
88EXPORT_SYMBOL(pm_power_off);
89
90void show_regs(struct pt_regs * regs)
91{
Geert Uytterhoeven7c79e1e2016-12-06 19:57:37 +010092 pr_info("Format %02x Vector: %04x PC: %08lx Status: %04x %s\n",
93 regs->format, regs->vector, regs->pc, regs->sr,
94 print_tainted());
95 pr_info("ORIG_D0: %08lx D0: %08lx A2: %08lx A1: %08lx\n",
96 regs->orig_d0, regs->d0, regs->a2, regs->a1);
97 pr_info("A0: %08lx D5: %08lx D4: %08lx\n", regs->a0, regs->d5,
98 regs->d4);
99 pr_info("D3: %08lx D2: %08lx D1: %08lx\n", regs->d3, regs->d2,
100 regs->d1);
Greg Ungererfde39442012-02-09 14:18:46 +1000101 if (!(regs->sr & PS_S))
Geert Uytterhoeven7c79e1e2016-12-06 19:57:37 +0100102 pr_info("USP: %08lx\n", rdusp());
Greg Ungererfde39442012-02-09 14:18:46 +1000103}
104
Greg Ungererfde39442012-02-09 14:18:46 +1000105void flush_thread(void)
106{
107 current->thread.fs = __USER_DS;
108#ifdef CONFIG_FPU
109 if (!FPU_IS_EMU) {
110 unsigned long zero = 0;
111 asm volatile("frestore %0": :"m" (zero));
112 }
113#endif
114}
115
116/*
Al Viro20ecc912012-10-21 16:41:46 -0400117 * Why not generic sys_clone, you ask? m68k passes all arguments on stack.
118 * And we need all registers saved, which means a bunch of stuff pushed
119 * on top of pt_regs, which means that sys_clone() arguments would be
120 * buried. We could, of course, copy them, but it's too costly for no
121 * good reason - generic clone() would have to copy them *again* for
122 * do_fork() anyway. So in this case it's actually better to pass pt_regs *
123 * and extract arguments for do_fork() from there. Eventually we might
124 * go for calling do_fork() directly from the wrapper, but only after we
125 * are finished with do_fork() prototype conversion.
Greg Ungererfde39442012-02-09 14:18:46 +1000126 */
Greg Ungererfde39442012-02-09 14:18:46 +1000127asmlinkage int m68k_clone(struct pt_regs *regs)
128{
Al Viro20ecc912012-10-21 16:41:46 -0400129 /* regs will be equal to current_pt_regs() */
Al Viroe80d6662012-10-22 23:10:08 -0400130 return do_fork(regs->d1, regs->d2, 0,
Al Viro20ecc912012-10-21 16:41:46 -0400131 (int __user *)regs->d3, (int __user *)regs->d4);
Greg Ungererfde39442012-02-09 14:18:46 +1000132}
133
134int copy_thread(unsigned long clone_flags, unsigned long usp,
Al Viroafa86fc2012-10-22 22:51:14 -0400135 unsigned long arg, struct task_struct *p)
Greg Ungererfde39442012-02-09 14:18:46 +1000136{
Al Viro20ecc912012-10-21 16:41:46 -0400137 struct fork_frame {
138 struct switch_stack sw;
139 struct pt_regs regs;
140 } *frame;
Greg Ungererfde39442012-02-09 14:18:46 +1000141
Al Viro20ecc912012-10-21 16:41:46 -0400142 frame = (struct fork_frame *) (task_stack_page(p) + THREAD_SIZE) - 1;
Greg Ungererfde39442012-02-09 14:18:46 +1000143
Al Viro20ecc912012-10-21 16:41:46 -0400144 p->thread.ksp = (unsigned long)frame;
145 p->thread.esp0 = (unsigned long)&frame->regs;
Greg Ungererfde39442012-02-09 14:18:46 +1000146
147 /*
148 * Must save the current SFC/DFC value, NOT the value when
149 * the parent was last descheduled - RGH 10-08-96
150 */
151 p->thread.fs = get_fs().seg;
152
Al Viro20ecc912012-10-21 16:41:46 -0400153 if (unlikely(p->flags & PF_KTHREAD)) {
Al Viro533e6902012-09-16 12:05:09 -0400154 /* kernel thread */
Al Viro20ecc912012-10-21 16:41:46 -0400155 memset(frame, 0, sizeof(struct fork_frame));
156 frame->regs.sr = PS_S;
157 frame->sw.a3 = usp; /* function */
158 frame->sw.d7 = arg;
159 frame->sw.retpc = (unsigned long)ret_from_kernel_thread;
Al Viro533e6902012-09-16 12:05:09 -0400160 p->thread.usp = 0;
161 return 0;
162 }
Al Viro20ecc912012-10-21 16:41:46 -0400163 memcpy(frame, container_of(current_pt_regs(), struct fork_frame, regs),
164 sizeof(struct fork_frame));
165 frame->regs.d0 = 0;
166 frame->sw.retpc = (unsigned long)ret_from_fork;
167 p->thread.usp = usp ?: rdusp();
Al Viro533e6902012-09-16 12:05:09 -0400168
169 if (clone_flags & CLONE_SETTLS)
Al Viro20ecc912012-10-21 16:41:46 -0400170 task_thread_info(p)->tp_value = frame->regs.d5;
Al Viro533e6902012-09-16 12:05:09 -0400171
Greg Ungererfde39442012-02-09 14:18:46 +1000172#ifdef CONFIG_FPU
173 if (!FPU_IS_EMU) {
174 /* Copy the current fpu state */
175 asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
176
177 if (!CPU_IS_060 ? p->thread.fpstate[0] : p->thread.fpstate[2]) {
178 if (CPU_IS_COLDFIRE) {
179 asm volatile ("fmovemd %/fp0-%/fp7,%0\n\t"
180 "fmovel %/fpiar,%1\n\t"
181 "fmovel %/fpcr,%2\n\t"
182 "fmovel %/fpsr,%3"
183 :
184 : "m" (p->thread.fp[0]),
185 "m" (p->thread.fpcntl[0]),
186 "m" (p->thread.fpcntl[1]),
187 "m" (p->thread.fpcntl[2])
188 : "memory");
189 } else {
190 asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
191 "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
192 :
193 : "m" (p->thread.fp[0]),
194 "m" (p->thread.fpcntl[0])
195 : "memory");
196 }
197 }
198
199 /* Restore the state in case the fpu was busy */
200 asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
201 }
202#endif /* CONFIG_FPU */
203
204 return 0;
205}
206
207/* Fill in the fpu structure for a core dump. */
Greg Ungererfde39442012-02-09 14:18:46 +1000208int dump_fpu (struct pt_regs *regs, struct user_m68kfp_struct *fpu)
209{
Greg Ungererfde39442012-02-09 14:18:46 +1000210 if (FPU_IS_EMU) {
211 int i;
212
213 memcpy(fpu->fpcntl, current->thread.fpcntl, 12);
214 memcpy(fpu->fpregs, current->thread.fp, 96);
215 /* Convert internal fpu reg representation
216 * into long double format
217 */
218 for (i = 0; i < 24; i += 3)
219 fpu->fpregs[i] = ((fpu->fpregs[i] & 0xffff0000) << 15) |
220 ((fpu->fpregs[i] & 0x0000ffff) << 16);
221 return 1;
222 }
223
Greg Ungerer8912eac2016-08-29 16:43:51 +1000224 if (IS_ENABLED(CONFIG_FPU)) {
225 char fpustate[216];
Greg Ungererfde39442012-02-09 14:18:46 +1000226
Greg Ungerer8912eac2016-08-29 16:43:51 +1000227 /* First dump the fpu context to avoid protocol violation. */
228 asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
229 if (!CPU_IS_060 ? !fpustate[0] : !fpustate[2])
230 return 0;
231
232 if (CPU_IS_COLDFIRE) {
233 asm volatile ("fmovel %/fpiar,%0\n\t"
234 "fmovel %/fpcr,%1\n\t"
235 "fmovel %/fpsr,%2\n\t"
236 "fmovemd %/fp0-%/fp7,%3"
237 :
238 : "m" (fpu->fpcntl[0]),
239 "m" (fpu->fpcntl[1]),
240 "m" (fpu->fpcntl[2]),
241 "m" (fpu->fpregs[0])
242 : "memory");
243 } else {
244 asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
245 :
246 : "m" (fpu->fpcntl[0])
247 : "memory");
248 asm volatile ("fmovemx %/fp0-%/fp7,%0"
249 :
250 : "m" (fpu->fpregs[0])
251 : "memory");
252 }
Greg Ungererfde39442012-02-09 14:18:46 +1000253 }
254
255 return 1;
256}
257EXPORT_SYMBOL(dump_fpu);
Greg Ungererfde39442012-02-09 14:18:46 +1000258
Greg Ungererfde39442012-02-09 14:18:46 +1000259unsigned long get_wchan(struct task_struct *p)
260{
261 unsigned long fp, pc;
262 unsigned long stack_page;
263 int count = 0;
264 if (!p || p == current || p->state == TASK_RUNNING)
265 return 0;
266
267 stack_page = (unsigned long)task_stack_page(p);
268 fp = ((struct switch_stack *)p->thread.ksp)->a6;
269 do {
270 if (fp < stack_page+sizeof(struct thread_info) ||
271 fp >= 8184+stack_page)
272 return 0;
273 pc = ((unsigned long *)fp)[1];
274 if (!in_sched_functions(pc))
275 return pc;
276 fp = *(unsigned long *) fp;
277 } while (count++ < 16);
278 return 0;
279}