xref: /linux/arch/sh/kernel/process_32.c (revision 7025bec9125b0a02edcaf22c2dce753bf2c95480)
1 /*
2  * arch/sh/kernel/process.c
3  *
4  * This file handles the architecture-dependent parts of process handling..
5  *
6  *  Copyright (C) 1995  Linus Torvalds
7  *
8  *  SuperH version:  Copyright (C) 1999, 2000  Niibe Yutaka & Kaz Kojima
9  *		     Copyright (C) 2006 Lineo Solutions Inc. support SH4A UBC
10  *		     Copyright (C) 2002 - 2008  Paul Mundt
11  *
12  * This file is subject to the terms and conditions of the GNU General Public
13  * License.  See the file "COPYING" in the main directory of this archive
14  * for more details.
15  */
16 #include <linux/module.h>
17 #include <linux/mm.h>
18 #include <linux/elfcore.h>
19 #include <linux/pm.h>
20 #include <linux/kallsyms.h>
21 #include <linux/kexec.h>
22 #include <linux/kdebug.h>
23 #include <linux/tick.h>
24 #include <linux/reboot.h>
25 #include <linux/fs.h>
26 #include <linux/ftrace.h>
27 #include <linux/preempt.h>
28 #include <linux/hw_breakpoint.h>
29 #include <asm/uaccess.h>
30 #include <asm/mmu_context.h>
31 #include <asm/pgalloc.h>
32 #include <asm/system.h>
33 #include <asm/fpu.h>
34 #include <asm/syscalls.h>
35 #include <asm/watchdog.h>
36 
37 #ifdef CONFIG_32BIT
38 static void watchdog_trigger_immediate(void)
39 {
40 	sh_wdt_write_cnt(0xFF);
41 	sh_wdt_write_csr(0xC2);
42 }
43 
44 void machine_restart(char * __unused)
45 {
46 	local_irq_disable();
47 
48 	/* Use watchdog timer to trigger reset */
49 	watchdog_trigger_immediate();
50 
51 	while (1)
52 		cpu_sleep();
53 }
54 #else
55 void machine_restart(char * __unused)
56 {
57 	/* SR.BL=1 and invoke address error to let CPU reset (manual reset) */
58 	asm volatile("ldc %0, sr\n\t"
59 		     "mov.l @%1, %0" : : "r" (0x10000000), "r" (0x80000001));
60 }
61 #endif
62 
63 void machine_halt(void)
64 {
65 	local_irq_disable();
66 
67 	while (1)
68 		cpu_sleep();
69 }
70 
71 void machine_power_off(void)
72 {
73 	if (pm_power_off)
74 		pm_power_off();
75 }
76 
77 void show_regs(struct pt_regs * regs)
78 {
79 	printk("\n");
80 	printk("Pid : %d, Comm: \t\t%s\n", task_pid_nr(current), current->comm);
81 	printk("CPU : %d        \t\t%s  (%s %.*s)\n\n",
82 	       smp_processor_id(), print_tainted(), init_utsname()->release,
83 	       (int)strcspn(init_utsname()->version, " "),
84 	       init_utsname()->version);
85 
86 	print_symbol("PC is at %s\n", instruction_pointer(regs));
87 	print_symbol("PR is at %s\n", regs->pr);
88 
89 	printk("PC  : %08lx SP  : %08lx SR  : %08lx ",
90 	       regs->pc, regs->regs[15], regs->sr);
91 #ifdef CONFIG_MMU
92 	printk("TEA : %08x\n", ctrl_inl(MMU_TEA));
93 #else
94 	printk("\n");
95 #endif
96 
97 	printk("R0  : %08lx R1  : %08lx R2  : %08lx R3  : %08lx\n",
98 	       regs->regs[0],regs->regs[1],
99 	       regs->regs[2],regs->regs[3]);
100 	printk("R4  : %08lx R5  : %08lx R6  : %08lx R7  : %08lx\n",
101 	       regs->regs[4],regs->regs[5],
102 	       regs->regs[6],regs->regs[7]);
103 	printk("R8  : %08lx R9  : %08lx R10 : %08lx R11 : %08lx\n",
104 	       regs->regs[8],regs->regs[9],
105 	       regs->regs[10],regs->regs[11]);
106 	printk("R12 : %08lx R13 : %08lx R14 : %08lx\n",
107 	       regs->regs[12],regs->regs[13],
108 	       regs->regs[14]);
109 	printk("MACH: %08lx MACL: %08lx GBR : %08lx PR  : %08lx\n",
110 	       regs->mach, regs->macl, regs->gbr, regs->pr);
111 
112 	show_trace(NULL, (unsigned long *)regs->regs[15], regs);
113 	show_code(regs);
114 }
115 
116 /*
117  * Create a kernel thread
118  */
119 ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *))
120 {
121 	do_exit(fn(arg));
122 }
123 
124 /* Don't use this in BL=1(cli).  Or else, CPU resets! */
125 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
126 {
127 	struct pt_regs regs;
128 	int pid;
129 
130 	memset(&regs, 0, sizeof(regs));
131 	regs.regs[4] = (unsigned long)arg;
132 	regs.regs[5] = (unsigned long)fn;
133 
134 	regs.pc = (unsigned long)kernel_thread_helper;
135 	regs.sr = SR_MD;
136 #if defined(CONFIG_SH_FPU)
137 	regs.sr |= SR_FD;
138 #endif
139 
140 	/* Ok, create the new process.. */
141 	pid = do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0,
142 		      &regs, 0, NULL, NULL);
143 
144 	return pid;
145 }
146 EXPORT_SYMBOL(kernel_thread);
147 
148 /*
149  * Free current thread data structures etc..
150  */
151 void exit_thread(void)
152 {
153 }
154 
155 void flush_thread(void)
156 {
157 	struct task_struct *tsk = current;
158 
159 	flush_ptrace_hw_breakpoint(tsk);
160 
161 #if defined(CONFIG_SH_FPU)
162 	/* Forget lazy FPU state */
163 	clear_fpu(tsk, task_pt_regs(tsk));
164 	clear_used_math();
165 #endif
166 }
167 
168 void release_thread(struct task_struct *dead_task)
169 {
170 	/* do nothing */
171 }
172 
173 /* Fill in the fpu structure for a core dump.. */
174 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
175 {
176 	int fpvalid = 0;
177 
178 #if defined(CONFIG_SH_FPU)
179 	struct task_struct *tsk = current;
180 
181 	fpvalid = !!tsk_used_math(tsk);
182 	if (fpvalid)
183 		fpvalid = !fpregs_get(tsk, NULL, 0,
184 				      sizeof(struct user_fpu_struct),
185 				      fpu, NULL);
186 #endif
187 
188 	return fpvalid;
189 }
190 EXPORT_SYMBOL(dump_fpu);
191 
192 /*
193  * This gets called before we allocate a new thread and copy
194  * the current task into it.
195  */
196 void prepare_to_copy(struct task_struct *tsk)
197 {
198 	unlazy_fpu(tsk, task_pt_regs(tsk));
199 }
200 
201 asmlinkage void ret_from_fork(void);
202 
203 int copy_thread(unsigned long clone_flags, unsigned long usp,
204 		unsigned long unused,
205 		struct task_struct *p, struct pt_regs *regs)
206 {
207 	struct thread_info *ti = task_thread_info(p);
208 	struct pt_regs *childregs;
209 
210 #if defined(CONFIG_SH_DSP)
211 	struct task_struct *tsk = current;
212 
213 	if (is_dsp_enabled(tsk)) {
214 		/* We can use the __save_dsp or just copy the struct:
215 		 * __save_dsp(p);
216 		 * p->thread.dsp_status.status |= SR_DSP
217 		 */
218 		p->thread.dsp_status = tsk->thread.dsp_status;
219 	}
220 #endif
221 
222 	childregs = task_pt_regs(p);
223 	*childregs = *regs;
224 
225 	if (user_mode(regs)) {
226 		childregs->regs[15] = usp;
227 		ti->addr_limit = USER_DS;
228 	} else {
229 		childregs->regs[15] = (unsigned long)childregs;
230 		ti->addr_limit = KERNEL_DS;
231 		ti->status &= ~TS_USEDFPU;
232 		p->fpu_counter = 0;
233 	}
234 
235 	if (clone_flags & CLONE_SETTLS)
236 		childregs->gbr = childregs->regs[0];
237 
238 	childregs->regs[0] = 0; /* Set return value for child */
239 
240 	p->thread.sp = (unsigned long) childregs;
241 	p->thread.pc = (unsigned long) ret_from_fork;
242 
243 	memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
244 
245 	return 0;
246 }
247 
248 /*
249  *	switch_to(x,y) should switch tasks from x to y.
250  *
251  */
252 __notrace_funcgraph struct task_struct *
253 __switch_to(struct task_struct *prev, struct task_struct *next)
254 {
255 	struct thread_struct *next_t = &next->thread;
256 
257 	unlazy_fpu(prev, task_pt_regs(prev));
258 
259 	/* we're going to use this soon, after a few expensive things */
260 	if (next->fpu_counter > 5)
261 		prefetch(&next_t->fpu.hard);
262 
263 #ifdef CONFIG_MMU
264 	/*
265 	 * Restore the kernel mode register
266 	 *	k7 (r7_bank1)
267 	 */
268 	asm volatile("ldc	%0, r7_bank"
269 		     : /* no output */
270 		     : "r" (task_thread_info(next)));
271 #endif
272 
273 	/*
274 	 * If the task has used fpu the last 5 timeslices, just do a full
275 	 * restore of the math state immediately to avoid the trap; the
276 	 * chances of needing FPU soon are obviously high now
277 	 */
278 	if (next->fpu_counter > 5)
279 		fpu_state_restore(task_pt_regs(next));
280 
281 	return prev;
282 }
283 
284 asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
285 			unsigned long r6, unsigned long r7,
286 			struct pt_regs __regs)
287 {
288 #ifdef CONFIG_MMU
289 	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
290 	return do_fork(SIGCHLD, regs->regs[15], regs, 0, NULL, NULL);
291 #else
292 	/* fork almost works, enough to trick you into looking elsewhere :-( */
293 	return -EINVAL;
294 #endif
295 }
296 
297 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
298 			 unsigned long parent_tidptr,
299 			 unsigned long child_tidptr,
300 			 struct pt_regs __regs)
301 {
302 	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
303 	if (!newsp)
304 		newsp = regs->regs[15];
305 	return do_fork(clone_flags, newsp, regs, 0,
306 			(int __user *)parent_tidptr,
307 			(int __user *)child_tidptr);
308 }
309 
310 /*
311  * This is trivial, and on the face of it looks like it
312  * could equally well be done in user mode.
313  *
314  * Not so, for quite unobvious reasons - register pressure.
315  * In user mode vfork() cannot have a stack frame, and if
316  * done by calling the "clone()" system call directly, you
317  * do not have enough call-clobbered registers to hold all
318  * the information you need.
319  */
320 asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
321 			 unsigned long r6, unsigned long r7,
322 			 struct pt_regs __regs)
323 {
324 	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
325 	return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->regs[15], regs,
326 		       0, NULL, NULL);
327 }
328 
329 /*
330  * sys_execve() executes a new program.
331  */
332 asmlinkage int sys_execve(char __user *ufilename, char __user * __user *uargv,
333 			  char __user * __user *uenvp, unsigned long r7,
334 			  struct pt_regs __regs)
335 {
336 	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
337 	int error;
338 	char *filename;
339 
340 	filename = getname(ufilename);
341 	error = PTR_ERR(filename);
342 	if (IS_ERR(filename))
343 		goto out;
344 
345 	error = do_execve(filename, uargv, uenvp, regs);
346 	putname(filename);
347 out:
348 	return error;
349 }
350 
351 unsigned long get_wchan(struct task_struct *p)
352 {
353 	unsigned long pc;
354 
355 	if (!p || p == current || p->state == TASK_RUNNING)
356 		return 0;
357 
358 	/*
359 	 * The same comment as on the Alpha applies here, too ...
360 	 */
361 	pc = thread_saved_pc(p);
362 
363 #ifdef CONFIG_FRAME_POINTER
364 	if (in_sched_functions(pc)) {
365 		unsigned long schedule_frame = (unsigned long)p->thread.sp;
366 		return ((unsigned long *)schedule_frame)[21];
367 	}
368 #endif
369 
370 	return pc;
371 }
372