blob: b6f43f0ea743576af3235c33db784312d829267c [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>
18#include <linux/elfcore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kallsyms.h>
Paul Mundte06c4e52007-07-31 13:01:43 +090020#include <linux/fs.h>
Matt Fleming7816fec2009-07-11 00:29:04 +000021#include <linux/ftrace.h>
Paul Mundt09a07292009-11-09 16:27:40 +090022#include <linux/hw_breakpoint.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
24#include <asm/mmu_context.h>
Paul Mundtbd079992007-05-08 14:50:59 +090025#include <asm/system.h>
Paul Mundt9bbafce2008-03-26 19:02:47 +090026#include <asm/fpu.h>
Paul Mundtfa439722008-09-04 18:53:58 +090027#include <asm/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029void show_regs(struct pt_regs * regs)
30{
31 printk("\n");
Paul Mundt75fd24c2008-11-26 15:20:35 +090032 printk("Pid : %d, Comm: \t\t%s\n", task_pid_nr(current), current->comm);
33 printk("CPU : %d \t\t%s (%s %.*s)\n\n",
Paul Mundt7d961692008-08-08 01:23:34 +090034 smp_processor_id(), print_tainted(), init_utsname()->release,
35 (int)strcspn(init_utsname()->version, " "),
36 init_utsname()->version);
37
Paul Mundt6b002232006-10-12 17:07:45 +090038 print_symbol("PC is at %s\n", instruction_pointer(regs));
Paul Mundt7d961692008-08-08 01:23:34 +090039 print_symbol("PR is at %s\n", regs->pr);
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 printk("PC : %08lx SP : %08lx SR : %08lx ",
42 regs->pc, regs->regs[15], regs->sr);
43#ifdef CONFIG_MMU
Paul Mundt7d961692008-08-08 01:23:34 +090044 printk("TEA : %08x\n", ctrl_inl(MMU_TEA));
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#else
Paul Mundt7d961692008-08-08 01:23:34 +090046 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 printk("R0 : %08lx R1 : %08lx R2 : %08lx R3 : %08lx\n",
50 regs->regs[0],regs->regs[1],
51 regs->regs[2],regs->regs[3]);
52 printk("R4 : %08lx R5 : %08lx R6 : %08lx R7 : %08lx\n",
53 regs->regs[4],regs->regs[5],
54 regs->regs[6],regs->regs[7]);
55 printk("R8 : %08lx R9 : %08lx R10 : %08lx R11 : %08lx\n",
56 regs->regs[8],regs->regs[9],
57 regs->regs[10],regs->regs[11]);
58 printk("R12 : %08lx R13 : %08lx R14 : %08lx\n",
59 regs->regs[12],regs->regs[13],
60 regs->regs[14]);
61 printk("MACH: %08lx MACL: %08lx GBR : %08lx PR : %08lx\n",
62 regs->mach, regs->macl, regs->gbr, regs->pr);
63
Paul Mundt6b002232006-10-12 17:07:45 +090064 show_trace(NULL, (unsigned long *)regs->regs[15], regs);
Paul Mundt9cfc9a92008-11-26 14:31:03 +090065 show_code(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
68/*
69 * Create a kernel thread
70 */
Paul Mundt43f8f9b2008-12-17 12:20:15 +090071ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *))
72{
73 do_exit(fn(arg));
74}
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Paul Mundtaec5e0e2006-12-25 09:51:47 +090076/* Don't use this in BL=1(cli). Or else, CPU resets! */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
Paul Mundtaec5e0e2006-12-25 09:51:47 +090078{
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 struct pt_regs regs;
Paul Mundt3d586952008-09-21 13:56:39 +090080 int pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 memset(&regs, 0, sizeof(regs));
Paul Mundtaec5e0e2006-12-25 09:51:47 +090083 regs.regs[4] = (unsigned long)arg;
84 regs.regs[5] = (unsigned long)fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Paul Mundtaec5e0e2006-12-25 09:51:47 +090086 regs.pc = (unsigned long)kernel_thread_helper;
Stuart Menefyd3ea9fa2009-09-25 18:25:10 +010087 regs.sr = SR_MD;
88#if defined(CONFIG_SH_FPU)
89 regs.sr |= SR_FD;
90#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 /* Ok, create the new process.. */
Paul Mundt3d586952008-09-21 13:56:39 +090093 pid = do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0,
94 &regs, 0, NULL, NULL);
95
Paul Mundt3d586952008-09-21 13:56:39 +090096 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
Paul Mundt4c978ca32009-10-27 11:51:19 +090098EXPORT_SYMBOL(kernel_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Paul Mundt70e068e2010-01-12 18:52:00 +0900100void start_thread(struct pt_regs *regs, unsigned long new_pc,
101 unsigned long new_sp)
102{
103 set_fs(USER_DS);
104
105 regs->pr = 0;
106 regs->sr = SR_FD;
107 regs->pc = new_pc;
108 regs->regs[15] = new_sp;
Paul Mundt0ea820c2010-01-13 12:51:40 +0900109
110 free_thread_xstate(current);
Paul Mundt70e068e2010-01-12 18:52:00 +0900111}
112EXPORT_SYMBOL(start_thread);
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/*
115 * Free current thread data structures etc..
116 */
117void exit_thread(void)
118{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121void flush_thread(void)
122{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct task_struct *tsk = current;
Paul Mundt09a07292009-11-09 16:27:40 +0900124
125 flush_ptrace_hw_breakpoint(tsk);
126
127#if defined(CONFIG_SH_FPU)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 /* Forget lazy FPU state */
Al Viro3cf0f4e2006-01-12 01:05:44 -0800129 clear_fpu(tsk, task_pt_regs(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 clear_used_math();
131#endif
132}
133
134void release_thread(struct task_struct *dead_task)
135{
136 /* do nothing */
137}
138
139/* Fill in the fpu structure for a core dump.. */
140int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
141{
142 int fpvalid = 0;
143
144#if defined(CONFIG_SH_FPU)
145 struct task_struct *tsk = current;
146
147 fpvalid = !!tsk_used_math(tsk);
Paul Mundte7ab3cd2008-09-21 19:04:55 +0900148 if (fpvalid)
149 fpvalid = !fpregs_get(tsk, NULL, 0,
150 sizeof(struct user_fpu_struct),
151 fpu, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#endif
153
154 return fpvalid;
155}
Paul Mundt4c978ca32009-10-27 11:51:19 +0900156EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Stuart Menefyd3ea9fa2009-09-25 18:25:10 +0100158/*
159 * This gets called before we allocate a new thread and copy
160 * the current task into it.
161 */
162void prepare_to_copy(struct task_struct *tsk)
163{
164 unlazy_fpu(tsk, task_pt_regs(tsk));
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167asmlinkage void ret_from_fork(void);
168
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700169int copy_thread(unsigned long clone_flags, unsigned long usp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 unsigned long unused,
171 struct task_struct *p, struct pt_regs *regs)
172{
Paul Mundt2991be72006-09-27 17:07:07 +0900173 struct thread_info *ti = task_thread_info(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 struct pt_regs *childregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Michael Trimarchi01ab1032009-04-03 17:32:33 +0000176#if defined(CONFIG_SH_DSP)
Paul Mundt6424db52009-12-08 15:47:12 +0900177 struct task_struct *tsk = current;
178
Michael Trimarchi01ab1032009-04-03 17:32:33 +0000179 if (is_dsp_enabled(tsk)) {
180 /* We can use the __save_dsp or just copy the struct:
181 * __save_dsp(p);
182 * p->thread.dsp_status.status |= SR_DSP
183 */
184 p->thread.dsp_status = tsk->thread.dsp_status;
185 }
186#endif
187
Al Viro3cf0f4e2006-01-12 01:05:44 -0800188 childregs = task_pt_regs(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 *childregs = *regs;
190
191 if (user_mode(regs)) {
192 childregs->regs[15] = usp;
Paul Mundt2991be72006-09-27 17:07:07 +0900193 ti->addr_limit = USER_DS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 } else {
Hideo Saitoe6bcf562007-02-28 18:35:42 +0900195 childregs->regs[15] = (unsigned long)childregs;
Paul Mundt2991be72006-09-27 17:07:07 +0900196 ti->addr_limit = KERNEL_DS;
Stuart Menefyd3ea9fa2009-09-25 18:25:10 +0100197 ti->status &= ~TS_USEDFPU;
198 p->fpu_counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900200
Hideo Saitoe6bcf562007-02-28 18:35:42 +0900201 if (clone_flags & CLONE_SETTLS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 childregs->gbr = childregs->regs[0];
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 childregs->regs[0] = 0; /* Set return value for child */
205
206 p->thread.sp = (unsigned long) childregs;
207 p->thread.pc = (unsigned long) ret_from_fork;
208
Paul Mundt09a07292009-11-09 16:27:40 +0900209 memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 return 0;
212}
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
215 * switch_to(x,y) should switch tasks from x to y.
216 *
217 */
Matt Fleming7816fec2009-07-11 00:29:04 +0000218__notrace_funcgraph struct task_struct *
219__switch_to(struct task_struct *prev, struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Giuseppe CAVALLAROa0458b072009-07-07 16:25:10 +0200221 struct thread_struct *next_t = &next->thread;
222
Al Viro3cf0f4e2006-01-12 01:05:44 -0800223 unlazy_fpu(prev, task_pt_regs(prev));
Giuseppe CAVALLAROa0458b072009-07-07 16:25:10 +0200224
225 /* we're going to use this soon, after a few expensive things */
226 if (next->fpu_counter > 5)
Paul Mundt0ea820c2010-01-13 12:51:40 +0900227 prefetch(next_t->xstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Yoshinori Satoa2d1a5f2006-09-27 17:25:07 +0900229#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /*
231 * Restore the kernel mode register
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900232 * k7 (r7_bank1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
234 asm volatile("ldc %0, r7_bank"
235 : /* no output */
Al Virocafcfca2006-01-12 01:05:45 -0800236 : "r" (task_thread_info(next)));
Yoshinori Satoa2d1a5f2006-09-27 17:25:07 +0900237#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Paul Mundt6ba65382009-11-25 12:07:31 +0900239 /*
240 * If the task has used fpu the last 5 timeslices, just do a full
Giuseppe CAVALLAROa0458b072009-07-07 16:25:10 +0200241 * restore of the math state immediately to avoid the trap; the
242 * chances of needing FPU soon are obviously high now
243 */
Paul Mundt6ba65382009-11-25 12:07:31 +0900244 if (next->fpu_counter > 5)
Paul Mundt0ea820c2010-01-13 12:51:40 +0900245 __fpu_state_restore();
Giuseppe CAVALLAROa0458b072009-07-07 16:25:10 +0200246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return prev;
248}
249
250asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
251 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900252 struct pt_regs __regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254#ifdef CONFIG_MMU
Paul Mundt882c12c2007-05-14 17:26:34 +0900255 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900256 return do_fork(SIGCHLD, regs->regs[15], regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257#else
258 /* fork almost works, enough to trick you into looking elsewhere :-( */
259 return -EINVAL;
260#endif
261}
262
263asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
264 unsigned long parent_tidptr,
265 unsigned long child_tidptr,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900266 struct pt_regs __regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900268 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (!newsp)
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900270 newsp = regs->regs[15];
271 return do_fork(clone_flags, newsp, regs, 0,
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900272 (int __user *)parent_tidptr,
273 (int __user *)child_tidptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276/*
277 * This is trivial, and on the face of it looks like it
278 * could equally well be done in user mode.
279 *
280 * Not so, for quite unobvious reasons - register pressure.
281 * In user mode vfork() cannot have a stack frame, and if
282 * done by calling the "clone()" system call directly, you
283 * do not have enough call-clobbered registers to hold all
284 * the information you need.
285 */
286asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
287 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900288 struct pt_regs __regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900290 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
291 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->regs[15], regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 0, NULL, NULL);
293}
294
295/*
296 * sys_execve() executes a new program.
297 */
Paul Mundte08f4572007-05-14 12:52:56 +0900298asmlinkage int sys_execve(char __user *ufilename, char __user * __user *uargv,
299 char __user * __user *uenvp, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900300 struct pt_regs __regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900302 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 int error;
304 char *filename;
305
Paul Mundte08f4572007-05-14 12:52:56 +0900306 filename = getname(ufilename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 error = PTR_ERR(filename);
308 if (IS_ERR(filename))
309 goto out;
310
Paul Mundte08f4572007-05-14 12:52:56 +0900311 error = do_execve(filename, uargv, uenvp, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 putname(filename);
313out:
314 return error;
315}
316
317unsigned long get_wchan(struct task_struct *p)
318{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 unsigned long pc;
320
321 if (!p || p == current || p->state == TASK_RUNNING)
322 return 0;
323
324 /*
325 * The same comment as on the Alpha applies here, too ...
326 */
327 pc = thread_saved_pc(p);
David McCulloughc64ac9f2007-07-26 17:46:07 +0900328
329#ifdef CONFIG_FRAME_POINTER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (in_sched_functions(pc)) {
David McCulloughc64ac9f2007-07-26 17:46:07 +0900331 unsigned long schedule_frame = (unsigned long)p->thread.sp;
Paul Mundtb652c232006-12-08 17:46:29 +0900332 return ((unsigned long *)schedule_frame)[21];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
David McCulloughc64ac9f2007-07-26 17:46:07 +0900334#endif
Paul Mundtb652c232006-12-08 17:46:29 +0900335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return pc;
337}