]>
Commit | Line | Data |
---|---|---|
aec5e0e1 PM |
1 | /* |
2 | * arch/sh/kernel/process.c | |
1da177e4 | 3 | * |
aec5e0e1 | 4 | * This file handles the architecture-dependent parts of process handling.. |
1da177e4 LT |
5 | * |
6 | * Copyright (C) 1995 Linus Torvalds | |
7 | * | |
8 | * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima | |
8ae91b9a | 9 | * Copyright (C) 2006 Lineo Solutions Inc. support SH4A UBC |
e7ab3cd2 PM |
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. | |
1da177e4 | 15 | */ |
1da177e4 | 16 | #include <linux/module.h> |
1da177e4 LT |
17 | #include <linux/mm.h> |
18 | #include <linux/elfcore.h> | |
1da177e4 | 19 | #include <linux/kallsyms.h> |
e06c4e57 | 20 | #include <linux/fs.h> |
7816fecd | 21 | #include <linux/ftrace.h> |
09a07294 | 22 | #include <linux/hw_breakpoint.h> |
1da177e4 LT |
23 | #include <asm/uaccess.h> |
24 | #include <asm/mmu_context.h> | |
bd079997 | 25 | #include <asm/system.h> |
9bbafce2 | 26 | #include <asm/fpu.h> |
fa43972f | 27 | #include <asm/syscalls.h> |
1da177e4 | 28 | |
1da177e4 LT |
29 | void show_regs(struct pt_regs * regs) |
30 | { | |
31 | printk("\n"); | |
75fd24c1 PM |
32 | 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", | |
7d96169c PM |
34 | smp_processor_id(), print_tainted(), init_utsname()->release, |
35 | (int)strcspn(init_utsname()->version, " "), | |
36 | init_utsname()->version); | |
37 | ||
6b002230 | 38 | print_symbol("PC is at %s\n", instruction_pointer(regs)); |
7d96169c PM |
39 | print_symbol("PR is at %s\n", regs->pr); |
40 | ||
1da177e4 LT |
41 | printk("PC : %08lx SP : %08lx SR : %08lx ", |
42 | regs->pc, regs->regs[15], regs->sr); | |
43 | #ifdef CONFIG_MMU | |
7d96169c | 44 | printk("TEA : %08x\n", ctrl_inl(MMU_TEA)); |
1da177e4 | 45 | #else |
7d96169c | 46 | printk("\n"); |
1da177e4 | 47 | #endif |
1da177e4 LT |
48 | |
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 | ||
6b002230 | 64 | show_trace(NULL, (unsigned long *)regs->regs[15], regs); |
9cfc9a9b | 65 | show_code(regs); |
1da177e4 LT |
66 | } |
67 | ||
68 | /* | |
69 | * Create a kernel thread | |
70 | */ | |
43f8f9b9 PM |
71 | ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *)) |
72 | { | |
73 | do_exit(fn(arg)); | |
74 | } | |
1da177e4 | 75 | |
aec5e0e1 | 76 | /* Don't use this in BL=1(cli). Or else, CPU resets! */ |
1da177e4 | 77 | int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags) |
aec5e0e1 | 78 | { |
1da177e4 | 79 | struct pt_regs regs; |
3d58695e | 80 | int pid; |
1da177e4 LT |
81 | |
82 | memset(®s, 0, sizeof(regs)); | |
aec5e0e1 PM |
83 | regs.regs[4] = (unsigned long)arg; |
84 | regs.regs[5] = (unsigned long)fn; | |
1da177e4 | 85 | |
aec5e0e1 | 86 | regs.pc = (unsigned long)kernel_thread_helper; |
d3ea9fa0 SM |
87 | regs.sr = SR_MD; |
88 | #if defined(CONFIG_SH_FPU) | |
89 | regs.sr |= SR_FD; | |
90 | #endif | |
1da177e4 LT |
91 | |
92 | /* Ok, create the new process.. */ | |
3d58695e PM |
93 | pid = do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, |
94 | ®s, 0, NULL, NULL); | |
95 | ||
3d58695e | 96 | return pid; |
1da177e4 | 97 | } |
4c978ca3 | 98 | EXPORT_SYMBOL(kernel_thread); |
1da177e4 | 99 | |
70e068ee PM |
100 | void 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; | |
0ea820cf PM |
109 | |
110 | free_thread_xstate(current); | |
70e068ee PM |
111 | } |
112 | EXPORT_SYMBOL(start_thread); | |
113 | ||
1da177e4 LT |
114 | /* |
115 | * Free current thread data structures etc.. | |
116 | */ | |
117 | void exit_thread(void) | |
118 | { | |
1da177e4 LT |
119 | } |
120 | ||
121 | void flush_thread(void) | |
122 | { | |
1da177e4 | 123 | struct task_struct *tsk = current; |
09a07294 PM |
124 | |
125 | flush_ptrace_hw_breakpoint(tsk); | |
126 | ||
127 | #if defined(CONFIG_SH_FPU) | |
1da177e4 | 128 | /* Forget lazy FPU state */ |
3cf0f4ec | 129 | clear_fpu(tsk, task_pt_regs(tsk)); |
1da177e4 LT |
130 | clear_used_math(); |
131 | #endif | |
132 | } | |
133 | ||
134 | void release_thread(struct task_struct *dead_task) | |
135 | { | |
136 | /* do nothing */ | |
137 | } | |
138 | ||
139 | /* Fill in the fpu structure for a core dump.. */ | |
140 | int 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); | |
e7ab3cd2 PM |
148 | if (fpvalid) |
149 | fpvalid = !fpregs_get(tsk, NULL, 0, | |
150 | sizeof(struct user_fpu_struct), | |
151 | fpu, NULL); | |
1da177e4 LT |
152 | #endif |
153 | ||
154 | return fpvalid; | |
155 | } | |
4c978ca3 | 156 | EXPORT_SYMBOL(dump_fpu); |
1da177e4 | 157 | |
d3ea9fa0 SM |
158 | /* |
159 | * This gets called before we allocate a new thread and copy | |
160 | * the current task into it. | |
161 | */ | |
162 | void prepare_to_copy(struct task_struct *tsk) | |
163 | { | |
164 | unlazy_fpu(tsk, task_pt_regs(tsk)); | |
165 | } | |
166 | ||
1da177e4 LT |
167 | asmlinkage void ret_from_fork(void); |
168 | ||
6f2c55b8 | 169 | int copy_thread(unsigned long clone_flags, unsigned long usp, |
1da177e4 LT |
170 | unsigned long unused, |
171 | struct task_struct *p, struct pt_regs *regs) | |
172 | { | |
2991be72 | 173 | struct thread_info *ti = task_thread_info(p); |
1da177e4 | 174 | struct pt_regs *childregs; |
1da177e4 | 175 | |
d3ea9fa0 | 176 | #if defined(CONFIG_SH_DSP) |
1da177e4 LT |
177 | struct task_struct *tsk = current; |
178 | ||
01ab1039 MT |
179 | 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 | ||
3cf0f4ec | 188 | childregs = task_pt_regs(p); |
1da177e4 LT |
189 | *childregs = *regs; |
190 | ||
191 | if (user_mode(regs)) { | |
192 | childregs->regs[15] = usp; | |
2991be72 | 193 | ti->addr_limit = USER_DS; |
1da177e4 | 194 | } else { |
e6bcf562 | 195 | childregs->regs[15] = (unsigned long)childregs; |
2991be72 | 196 | ti->addr_limit = KERNEL_DS; |
d3ea9fa0 SM |
197 | ti->status &= ~TS_USEDFPU; |
198 | p->fpu_counter = 0; | |
1da177e4 | 199 | } |
aec5e0e1 | 200 | |
e6bcf562 | 201 | if (clone_flags & CLONE_SETTLS) |
1da177e4 | 202 | childregs->gbr = childregs->regs[0]; |
aec5e0e1 | 203 | |
1da177e4 LT |
204 | 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 | ||
09a07294 | 209 | memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps)); |
1da177e4 LT |
210 | |
211 | return 0; | |
212 | } | |
213 | ||
1da177e4 LT |
214 | /* |
215 | * switch_to(x,y) should switch tasks from x to y. | |
216 | * | |
217 | */ | |
7816fecd MF |
218 | __notrace_funcgraph struct task_struct * |
219 | __switch_to(struct task_struct *prev, struct task_struct *next) | |
1da177e4 | 220 | { |
a0458b07 GC |
221 | struct thread_struct *next_t = &next->thread; |
222 | ||
3cf0f4ec | 223 | unlazy_fpu(prev, task_pt_regs(prev)); |
a0458b07 GC |
224 | |
225 | /* we're going to use this soon, after a few expensive things */ | |
226 | if (next->fpu_counter > 5) | |
0ea820cf | 227 | prefetch(next_t->xstate); |
1da177e4 | 228 | |
a2d1a5fa | 229 | #ifdef CONFIG_MMU |
1da177e4 LT |
230 | /* |
231 | * Restore the kernel mode register | |
aec5e0e1 | 232 | * k7 (r7_bank1) |
1da177e4 LT |
233 | */ |
234 | asm volatile("ldc %0, r7_bank" | |
235 | : /* no output */ | |
cafcfcaa | 236 | : "r" (task_thread_info(next))); |
a2d1a5fa | 237 | #endif |
1da177e4 | 238 | |
6ba65383 PM |
239 | /* |
240 | * If the task has used fpu the last 5 timeslices, just do a full | |
a0458b07 GC |
241 | * restore of the math state immediately to avoid the trap; the |
242 | * chances of needing FPU soon are obviously high now | |
243 | */ | |
6ba65383 | 244 | if (next->fpu_counter > 5) |
0ea820cf | 245 | __fpu_state_restore(); |
a0458b07 | 246 | |
1da177e4 LT |
247 | return prev; |
248 | } | |
249 | ||
250 | asmlinkage int sys_fork(unsigned long r4, unsigned long r5, | |
251 | unsigned long r6, unsigned long r7, | |
f0bc814c | 252 | struct pt_regs __regs) |
1da177e4 LT |
253 | { |
254 | #ifdef CONFIG_MMU | |
882c12c4 | 255 | struct pt_regs *regs = RELOC_HIDE(&__regs, 0); |
f0bc814c | 256 | return do_fork(SIGCHLD, regs->regs[15], regs, 0, NULL, NULL); |
1da177e4 LT |
257 | #else |
258 | /* fork almost works, enough to trick you into looking elsewhere :-( */ | |
259 | return -EINVAL; | |
260 | #endif | |
261 | } | |
262 | ||
263 | asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp, | |
264 | unsigned long parent_tidptr, | |
265 | unsigned long child_tidptr, | |
f0bc814c | 266 | struct pt_regs __regs) |
1da177e4 | 267 | { |
f0bc814c | 268 | struct pt_regs *regs = RELOC_HIDE(&__regs, 0); |
1da177e4 | 269 | if (!newsp) |
f0bc814c SM |
270 | newsp = regs->regs[15]; |
271 | return do_fork(clone_flags, newsp, regs, 0, | |
aec5e0e1 PM |
272 | (int __user *)parent_tidptr, |
273 | (int __user *)child_tidptr); | |
1da177e4 LT |
274 | } |
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 | */ | |
286 | asmlinkage int sys_vfork(unsigned long r4, unsigned long r5, | |
287 | unsigned long r6, unsigned long r7, | |
f0bc814c | 288 | struct pt_regs __regs) |
1da177e4 | 289 | { |
f0bc814c SM |
290 | struct pt_regs *regs = RELOC_HIDE(&__regs, 0); |
291 | return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->regs[15], regs, | |
1da177e4 LT |
292 | 0, NULL, NULL); |
293 | } | |
294 | ||
295 | /* | |
296 | * sys_execve() executes a new program. | |
297 | */ | |
e08f457c PM |
298 | asmlinkage int sys_execve(char __user *ufilename, char __user * __user *uargv, |
299 | char __user * __user *uenvp, unsigned long r7, | |
f0bc814c | 300 | struct pt_regs __regs) |
1da177e4 | 301 | { |
f0bc814c | 302 | struct pt_regs *regs = RELOC_HIDE(&__regs, 0); |
1da177e4 LT |
303 | int error; |
304 | char *filename; | |
305 | ||
e08f457c | 306 | filename = getname(ufilename); |
1da177e4 LT |
307 | error = PTR_ERR(filename); |
308 | if (IS_ERR(filename)) | |
309 | goto out; | |
310 | ||
e08f457c | 311 | error = do_execve(filename, uargv, uenvp, regs); |
1da177e4 LT |
312 | putname(filename); |
313 | out: | |
314 | return error; | |
315 | } | |
316 | ||
317 | unsigned long get_wchan(struct task_struct *p) | |
318 | { | |
1da177e4 LT |
319 | 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); | |
c64ac9f0 DM |
328 | |
329 | #ifdef CONFIG_FRAME_POINTER | |
1da177e4 | 330 | if (in_sched_functions(pc)) { |
c64ac9f0 | 331 | unsigned long schedule_frame = (unsigned long)p->thread.sp; |
b652c23c | 332 | return ((unsigned long *)schedule_frame)[21]; |
1da177e4 | 333 | } |
c64ac9f0 | 334 | #endif |
b652c23c | 335 | |
1da177e4 LT |
336 | return pc; |
337 | } |