blob: b473f0c06fbc11bf545f66b84bd0a566fb344286 [file] [log] [blame]
Paul Mundtaec5e0e2006-12-25 09:51:47 +09001/*
2 * arch/sh/kernel/process.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Paul Mundtaec5e0e2006-12-25 09:51:47 +09004 * This file handles the architecture-dependent parts of process handling..
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Copyright (C) 1995 Linus Torvalds
7 *
8 * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
Ryusuke Sakato8ae91b92006-10-12 12:16:13 +09009 * Copyright (C) 2006 Lineo Solutions Inc. support SH4A UBC
Paul Mundte7ab3cd2008-09-21 19:04:55 +090010 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/elfcore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/kallsyms.h>
Paul Mundte06c4e52007-07-31 13:01:43 +090021#include <linux/fs.h>
Matt Fleming7816fec2009-07-11 00:29:04 +000022#include <linux/ftrace.h>
Paul Mundt09a07292009-11-09 16:27:40 +090023#include <linux/hw_breakpoint.h>
Paul Mundt0f0ebd92011-05-24 17:25:23 +090024#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/uaccess.h>
26#include <asm/mmu_context.h>
Paul Mundtbd079992007-05-08 14:50:59 +090027#include <asm/system.h>
Paul Mundt9bbafce2008-03-26 19:02:47 +090028#include <asm/fpu.h>
Paul Mundtfa439722008-09-04 18:53:58 +090029#include <asm/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031void show_regs(struct pt_regs * regs)
32{
33 printk("\n");
Paul Mundt75fd24c2008-11-26 15:20:35 +090034 printk("Pid : %d, Comm: \t\t%s\n", task_pid_nr(current), current->comm);
35 printk("CPU : %d \t\t%s (%s %.*s)\n\n",
Paul Mundt7d961692008-08-08 01:23:34 +090036 smp_processor_id(), print_tainted(), init_utsname()->release,
37 (int)strcspn(init_utsname()->version, " "),
38 init_utsname()->version);
39
Paul Mundt6b002232006-10-12 17:07:45 +090040 print_symbol("PC is at %s\n", instruction_pointer(regs));
Paul Mundt7d961692008-08-08 01:23:34 +090041 print_symbol("PR is at %s\n", regs->pr);
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 printk("PC : %08lx SP : %08lx SR : %08lx ",
44 regs->pc, regs->regs[15], regs->sr);
45#ifdef CONFIG_MMU
Paul Mundt9d56dd32010-01-26 12:58:40 +090046 printk("TEA : %08x\n", __raw_readl(MMU_TEA));
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#else
Paul Mundt7d961692008-08-08 01:23:34 +090048 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 printk("R0 : %08lx R1 : %08lx R2 : %08lx R3 : %08lx\n",
52 regs->regs[0],regs->regs[1],
53 regs->regs[2],regs->regs[3]);
54 printk("R4 : %08lx R5 : %08lx R6 : %08lx R7 : %08lx\n",
55 regs->regs[4],regs->regs[5],
56 regs->regs[6],regs->regs[7]);
57 printk("R8 : %08lx R9 : %08lx R10 : %08lx R11 : %08lx\n",
58 regs->regs[8],regs->regs[9],
59 regs->regs[10],regs->regs[11]);
60 printk("R12 : %08lx R13 : %08lx R14 : %08lx\n",
61 regs->regs[12],regs->regs[13],
62 regs->regs[14]);
63 printk("MACH: %08lx MACL: %08lx GBR : %08lx PR : %08lx\n",
64 regs->mach, regs->macl, regs->gbr, regs->pr);
65
Paul Mundt6b002232006-10-12 17:07:45 +090066 show_trace(NULL, (unsigned long *)regs->regs[15], regs);
Paul Mundt9cfc9a92008-11-26 14:31:03 +090067 show_code(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70/*
71 * Create a kernel thread
72 */
Paul Mundt43f8f9b2008-12-17 12:20:15 +090073ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *))
74{
75 do_exit(fn(arg));
76}
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Paul Mundtaec5e0e2006-12-25 09:51:47 +090078/* Don't use this in BL=1(cli). Or else, CPU resets! */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
Paul Mundtaec5e0e2006-12-25 09:51:47 +090080{
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 struct pt_regs regs;
Paul Mundt3d586952008-09-21 13:56:39 +090082 int pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 memset(&regs, 0, sizeof(regs));
Paul Mundtaec5e0e2006-12-25 09:51:47 +090085 regs.regs[4] = (unsigned long)arg;
86 regs.regs[5] = (unsigned long)fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Paul Mundtaec5e0e2006-12-25 09:51:47 +090088 regs.pc = (unsigned long)kernel_thread_helper;
Stuart Menefyd3ea9fa2009-09-25 18:25:10 +010089 regs.sr = SR_MD;
90#if defined(CONFIG_SH_FPU)
91 regs.sr |= SR_FD;
92#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 /* Ok, create the new process.. */
Paul Mundt3d586952008-09-21 13:56:39 +090095 pid = do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0,
96 &regs, 0, NULL, NULL);
97
Paul Mundt3d586952008-09-21 13:56:39 +090098 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
Paul Mundt4c978ca2009-10-27 11:51:19 +0900100EXPORT_SYMBOL(kernel_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Paul Mundt70e068e2010-01-12 18:52:00 +0900102void start_thread(struct pt_regs *regs, unsigned long new_pc,
103 unsigned long new_sp)
104{
105 set_fs(USER_DS);
106
107 regs->pr = 0;
108 regs->sr = SR_FD;
109 regs->pc = new_pc;
110 regs->regs[15] = new_sp;
Paul Mundt0ea820c2010-01-13 12:51:40 +0900111
112 free_thread_xstate(current);
Paul Mundt70e068e2010-01-12 18:52:00 +0900113}
114EXPORT_SYMBOL(start_thread);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/*
117 * Free current thread data structures etc..
118 */
119void exit_thread(void)
120{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123void flush_thread(void)
124{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 struct task_struct *tsk = current;
Paul Mundt09a07292009-11-09 16:27:40 +0900126
127 flush_ptrace_hw_breakpoint(tsk);
128
129#if defined(CONFIG_SH_FPU)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /* Forget lazy FPU state */
Al Viro3cf0f4e2006-01-12 01:05:44 -0800131 clear_fpu(tsk, task_pt_regs(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 clear_used_math();
133#endif
134}
135
136void release_thread(struct task_struct *dead_task)
137{
138 /* do nothing */
139}
140
141/* Fill in the fpu structure for a core dump.. */
142int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
143{
144 int fpvalid = 0;
145
146#if defined(CONFIG_SH_FPU)
147 struct task_struct *tsk = current;
148
149 fpvalid = !!tsk_used_math(tsk);
Paul Mundte7ab3cd2008-09-21 19:04:55 +0900150 if (fpvalid)
151 fpvalid = !fpregs_get(tsk, NULL, 0,
152 sizeof(struct user_fpu_struct),
153 fpu, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#endif
155
156 return fpvalid;
157}
Paul Mundt4c978ca2009-10-27 11:51:19 +0900158EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Stuart Menefyd3ea9fa2009-09-25 18:25:10 +0100160/*
161 * This gets called before we allocate a new thread and copy
162 * the current task into it.
163 */
164void prepare_to_copy(struct task_struct *tsk)
165{
166 unlazy_fpu(tsk, task_pt_regs(tsk));
167}
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169asmlinkage void ret_from_fork(void);
170
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700171int copy_thread(unsigned long clone_flags, unsigned long usp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned long unused,
173 struct task_struct *p, struct pt_regs *regs)
174{
Paul Mundt2991be72006-09-27 17:07:07 +0900175 struct thread_info *ti = task_thread_info(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct pt_regs *childregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Michael Trimarchi01ab1032009-04-03 17:32:33 +0000178#if defined(CONFIG_SH_DSP)
Paul Mundt6424db52009-12-08 15:47:12 +0900179 struct task_struct *tsk = current;
180
Michael Trimarchi01ab1032009-04-03 17:32:33 +0000181 if (is_dsp_enabled(tsk)) {
182 /* We can use the __save_dsp or just copy the struct:
183 * __save_dsp(p);
184 * p->thread.dsp_status.status |= SR_DSP
185 */
186 p->thread.dsp_status = tsk->thread.dsp_status;
187 }
188#endif
189
Al Viro3cf0f4e2006-01-12 01:05:44 -0800190 childregs = task_pt_regs(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 *childregs = *regs;
192
193 if (user_mode(regs)) {
194 childregs->regs[15] = usp;
Paul Mundt2991be72006-09-27 17:07:07 +0900195 ti->addr_limit = USER_DS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 } else {
Hideo Saitoe6bcf562007-02-28 18:35:42 +0900197 childregs->regs[15] = (unsigned long)childregs;
Paul Mundt2991be72006-09-27 17:07:07 +0900198 ti->addr_limit = KERNEL_DS;
Stuart Menefyd3ea9fa2009-09-25 18:25:10 +0100199 ti->status &= ~TS_USEDFPU;
200 p->fpu_counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900202
Hideo Saitoe6bcf562007-02-28 18:35:42 +0900203 if (clone_flags & CLONE_SETTLS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 childregs->gbr = childregs->regs[0];
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 childregs->regs[0] = 0; /* Set return value for child */
207
208 p->thread.sp = (unsigned long) childregs;
209 p->thread.pc = (unsigned long) ret_from_fork;
210
Paul Mundt09a07292009-11-09 16:27:40 +0900211 memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 return 0;
214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
217 * switch_to(x,y) should switch tasks from x to y.
218 *
219 */
Matt Fleming7816fec2009-07-11 00:29:04 +0000220__notrace_funcgraph struct task_struct *
221__switch_to(struct task_struct *prev, struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Giuseppe CAVALLAROa0458b02009-07-07 16:25:10 +0200223 struct thread_struct *next_t = &next->thread;
224
Al Viro3cf0f4e2006-01-12 01:05:44 -0800225 unlazy_fpu(prev, task_pt_regs(prev));
Giuseppe CAVALLAROa0458b02009-07-07 16:25:10 +0200226
227 /* we're going to use this soon, after a few expensive things */
228 if (next->fpu_counter > 5)
Paul Mundt0ea820c2010-01-13 12:51:40 +0900229 prefetch(next_t->xstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Yoshinori Satoa2d1a5f2006-09-27 17:25:07 +0900231#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /*
233 * Restore the kernel mode register
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900234 * k7 (r7_bank1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
236 asm volatile("ldc %0, r7_bank"
237 : /* no output */
Al Virocafcfca2006-01-12 01:05:45 -0800238 : "r" (task_thread_info(next)));
Yoshinori Satoa2d1a5f2006-09-27 17:25:07 +0900239#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Paul Mundt6ba65382009-11-25 12:07:31 +0900241 /*
242 * If the task has used fpu the last 5 timeslices, just do a full
Giuseppe CAVALLAROa0458b02009-07-07 16:25:10 +0200243 * restore of the math state immediately to avoid the trap; the
244 * chances of needing FPU soon are obviously high now
245 */
Paul Mundt6ba65382009-11-25 12:07:31 +0900246 if (next->fpu_counter > 5)
Paul Mundt0ea820c2010-01-13 12:51:40 +0900247 __fpu_state_restore();
Giuseppe CAVALLAROa0458b02009-07-07 16:25:10 +0200248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return prev;
250}
251
252asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
253 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900254 struct pt_regs __regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256#ifdef CONFIG_MMU
Paul Mundt882c12c2007-05-14 17:26:34 +0900257 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900258 return do_fork(SIGCHLD, regs->regs[15], regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259#else
260 /* fork almost works, enough to trick you into looking elsewhere :-( */
261 return -EINVAL;
262#endif
263}
264
265asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
266 unsigned long parent_tidptr,
267 unsigned long child_tidptr,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900268 struct pt_regs __regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900270 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (!newsp)
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900272 newsp = regs->regs[15];
273 return do_fork(clone_flags, newsp, regs, 0,
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900274 (int __user *)parent_tidptr,
275 (int __user *)child_tidptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/*
279 * This is trivial, and on the face of it looks like it
280 * could equally well be done in user mode.
281 *
282 * Not so, for quite unobvious reasons - register pressure.
283 * In user mode vfork() cannot have a stack frame, and if
284 * done by calling the "clone()" system call directly, you
285 * do not have enough call-clobbered registers to hold all
286 * the information you need.
287 */
288asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
289 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900290 struct pt_regs __regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900292 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
293 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->regs[15], regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 0, NULL, NULL);
295}
296
297/*
298 * sys_execve() executes a new program.
299 */
David Howellsd7627462010-08-17 23:52:56 +0100300asmlinkage int sys_execve(const char __user *ufilename,
301 const char __user *const __user *uargv,
302 const char __user *const __user *uenvp,
303 unsigned long r7, struct pt_regs __regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900305 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int error;
307 char *filename;
308
Paul Mundte08f4572007-05-14 12:52:56 +0900309 filename = getname(ufilename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 error = PTR_ERR(filename);
311 if (IS_ERR(filename))
312 goto out;
313
Paul Mundte08f4572007-05-14 12:52:56 +0900314 error = do_execve(filename, uargv, uenvp, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 putname(filename);
316out:
317 return error;
318}
319
320unsigned long get_wchan(struct task_struct *p)
321{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 unsigned long pc;
323
324 if (!p || p == current || p->state == TASK_RUNNING)
325 return 0;
326
327 /*
328 * The same comment as on the Alpha applies here, too ...
329 */
330 pc = thread_saved_pc(p);
David McCulloughc64ac9f2007-07-26 17:46:07 +0900331
332#ifdef CONFIG_FRAME_POINTER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (in_sched_functions(pc)) {
David McCulloughc64ac9f2007-07-26 17:46:07 +0900334 unsigned long schedule_frame = (unsigned long)p->thread.sp;
Paul Mundtb652c232006-12-08 17:46:29 +0900335 return ((unsigned long *)schedule_frame)[21];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
David McCulloughc64ac9f2007-07-26 17:46:07 +0900337#endif
Paul Mundtb652c232006-12-08 17:46:29 +0900338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return pc;
340}