fork: move the real prepare_to_copy() users to arch_dup_task_struct()
[linux-2.6-block.git] / arch / frv / kernel / process.c
CommitLineData
1da177e4
LT
1/* process.c: FRV specific parts of process handling
2 *
3 * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 * - Derived from arch/m68k/kernel/process.c
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
9dec17eb 13#include <linux/module.h>
1da177e4
LT
14#include <linux/errno.h>
15#include <linux/sched.h>
16#include <linux/kernel.h>
17#include <linux/mm.h>
18#include <linux/smp.h>
1da177e4
LT
19#include <linux/stddef.h>
20#include <linux/unistd.h>
21#include <linux/ptrace.h>
22#include <linux/slab.h>
23#include <linux/user.h>
24#include <linux/elf.h>
25#include <linux/reboot.h>
26#include <linux/interrupt.h>
8defab33 27#include <linux/pagemap.h>
1da177e4 28
84e8cd6d 29#include <asm/asm-offsets.h>
1da177e4 30#include <asm/uaccess.h>
1da177e4
LT
31#include <asm/setup.h>
32#include <asm/pgtable.h>
8defab33 33#include <asm/tlb.h>
1da177e4
LT
34#include <asm/gdb-stub.h>
35#include <asm/mb-regs.h>
36
37#include "local.h"
38
39asmlinkage void ret_from_fork(void);
40
41#include <asm/pgalloc.h>
42
9dec17eb
DH
43void (*pm_power_off)(void);
44EXPORT_SYMBOL(pm_power_off);
45
504f52b5 46struct task_struct *alloc_task_struct_node(int node)
1da177e4 47{
504f52b5
ED
48 struct task_struct *p = kmalloc_node(THREAD_SIZE, GFP_KERNEL, node);
49
1da177e4
LT
50 if (p)
51 atomic_set((atomic_t *)(p+1), 1);
52 return p;
53}
54
55void free_task_struct(struct task_struct *p)
56{
57 if (atomic_dec_and_test((atomic_t *)(p+1)))
58 kfree(p);
59}
60
61static void core_sleep_idle(void)
62{
63#ifdef LED_DEBUG_SLEEP
64 /* Show that we're sleeping... */
65 __set_LEDS(0x55aa);
66#endif
67 frv_cpu_core_sleep();
68#ifdef LED_DEBUG_SLEEP
69 /* ... and that we woke up */
70 __set_LEDS(0);
71#endif
72 mb();
73}
74
75void (*idle)(void) = core_sleep_idle;
76
77/*
78 * The idle thread. There's no useful work to be
79 * done, so just try to conserve power and have a
80 * low exit latency (ie sit in a loop waiting for
81 * somebody to say that they'd like to reschedule)
82 */
83void cpu_idle(void)
84{
85 /* endless idle loop with no priority at all */
86 while (1) {
87 while (!need_resched()) {
8defab33
CL
88 check_pgt_cache();
89
1da177e4
LT
90 if (!frv_dma_inprogress && idle)
91 idle();
92 }
93
bd2f5536 94 schedule_preempt_disabled();
1da177e4
LT
95 }
96}
97
98void machine_restart(char * __unused)
99{
100 unsigned long reset_addr;
101#ifdef CONFIG_GDBSTUB
102 gdbstub_exit(0);
103#endif
104
105 if (PSR_IMPLE(__get_PSR()) == PSR_IMPLE_FR551)
106 reset_addr = 0xfefff500;
107 else
108 reset_addr = 0xfeff0500;
109
110 /* Software reset. */
111 asm volatile(" dcef @(gr0,gr0),1 ! membar !"
112 " sti %1,@(%0,0) !"
113 " nop ! nop ! nop ! nop ! nop ! "
114 " nop ! nop ! nop ! nop ! nop ! "
115 " nop ! nop ! nop ! nop ! nop ! "
116 " nop ! nop ! nop ! nop ! nop ! "
117 : : "r" (reset_addr), "r" (1) );
118
119 for (;;)
120 ;
121}
122
123void machine_halt(void)
124{
125#ifdef CONFIG_GDBSTUB
126 gdbstub_exit(0);
127#endif
128
129 for (;;);
130}
131
132void machine_power_off(void)
133{
134#ifdef CONFIG_GDBSTUB
135 gdbstub_exit(0);
136#endif
137
138 for (;;);
139}
140
141void flush_thread(void)
142{
adc400f6 143 /* nothing */
1da177e4
LT
144}
145
146inline unsigned long user_stack(const struct pt_regs *regs)
147{
148 while (regs->next_frame)
149 regs = regs->next_frame;
150 return user_mode(regs) ? regs->sp : 0;
151}
152
153asmlinkage int sys_fork(void)
154{
155#ifndef CONFIG_MMU
156 /* fork almost works, enough to trick you into looking elsewhere:-( */
157 return -EINVAL;
158#else
159 return do_fork(SIGCHLD, user_stack(__frame), __frame, 0, NULL, NULL);
160#endif
161}
162
163asmlinkage int sys_vfork(void)
164{
165 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, user_stack(__frame), __frame, 0,
166 NULL, NULL);
167}
168
169/*****************************************************************************/
170/*
171 * clone a process
172 * - tlsptr is retrieved by copy_thread()
173 */
174asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
175 int __user *parent_tidptr, int __user *child_tidptr,
176 int __user *tlsptr)
177{
178 if (!newsp)
179 newsp = user_stack(__frame);
180 return do_fork(clone_flags, newsp, __frame, 0, parent_tidptr, child_tidptr);
181} /* end sys_clone() */
182
1da177e4
LT
183/*
184 * set up the kernel stack and exception frames for a new process
185 */
6f2c55b8 186int copy_thread(unsigned long clone_flags,
1da177e4
LT
187 unsigned long usp, unsigned long topstk,
188 struct task_struct *p, struct pt_regs *regs)
189{
190 struct pt_regs *childregs0, *childregs, *regs0;
191
192 regs0 = __kernel_frame0_ptr;
193 childregs0 = (struct pt_regs *)
84e8cd6d 194 (task_stack_page(p) + THREAD_SIZE - FRV_FRAME0_SIZE);
1da177e4
LT
195 childregs = childregs0;
196
197 /* set up the userspace frame (the only place that the USP is stored) */
198 *childregs0 = *regs0;
199
200 childregs0->gr8 = 0;
201 childregs0->sp = usp;
202 childregs0->next_frame = NULL;
203
204 /* set up the return kernel frame if called from kernel_thread() */
205 if (regs != regs0) {
206 childregs--;
207 *childregs = *regs;
208 childregs->sp = (unsigned long) childregs0;
209 childregs->next_frame = childregs0;
097cb338 210 childregs->gr15 = (unsigned long) task_thread_info(p);
1da177e4
LT
211 childregs->gr29 = (unsigned long) p;
212 }
213
214 p->set_child_tid = p->clear_child_tid = NULL;
215
216 p->thread.frame = childregs;
217 p->thread.curr = p;
218 p->thread.sp = (unsigned long) childregs;
219 p->thread.fp = 0;
220 p->thread.lr = 0;
221 p->thread.pc = (unsigned long) ret_from_fork;
222 p->thread.frame0 = childregs0;
223
224 /* the new TLS pointer is passed in as arg #5 to sys_clone() */
225 if (clone_flags & CLONE_SETTLS)
226 childregs->gr29 = childregs->gr12;
227
228 save_user_regs(p->thread.user);
229
230 return 0;
231} /* end copy_thread() */
232
1da177e4
LT
233/*
234 * sys_execve() executes a new program.
235 */
d7627467
DH
236asmlinkage int sys_execve(const char __user *name,
237 const char __user *const __user *argv,
238 const char __user *const __user *envp)
1da177e4
LT
239{
240 int error;
241 char * filename;
242
1da177e4
LT
243 filename = getname(name);
244 error = PTR_ERR(filename);
245 if (IS_ERR(filename))
b69975a3 246 return error;
1da177e4
LT
247 error = do_execve(filename, argv, envp, __frame);
248 putname(filename);
1da177e4
LT
249 return error;
250}
251
252unsigned long get_wchan(struct task_struct *p)
253{
254 struct pt_regs *regs0;
255 unsigned long fp, pc;
256 unsigned long stack_limit;
257 int count = 0;
258 if (!p || p == current || p->state == TASK_RUNNING)
259 return 0;
260
261 stack_limit = (unsigned long) (p + 1);
262 fp = p->thread.fp;
263 regs0 = p->thread.frame0;
264
265 do {
266 if (fp < stack_limit || fp >= (unsigned long) regs0 || fp & 3)
267 return 0;
268
269 pc = ((unsigned long *) fp)[2];
270
271 /* FIXME: This depends on the order of these functions. */
272 if (!in_sched_functions(pc))
273 return pc;
274
275 fp = *(unsigned long *) fp;
276 } while (count++ < 16);
277
278 return 0;
279}
280
281unsigned long thread_saved_pc(struct task_struct *tsk)
282{
283 /* Check whether the thread is blocked in resume() */
284 if (in_sched_functions(tsk->thread.pc))
285 return ((unsigned long *)tsk->thread.fp)[2];
286 else
287 return tsk->thread.pc;
288}
289
290int elf_check_arch(const struct elf32_hdr *hdr)
291{
292 unsigned long hsr0 = __get_HSR(0);
293 unsigned long psr = __get_PSR();
294
295 if (hdr->e_machine != EM_FRV)
296 return 0;
297
298 switch (hdr->e_flags & EF_FRV_GPR_MASK) {
299 case EF_FRV_GPR64:
300 if ((hsr0 & HSR0_GRN) == HSR0_GRN_32)
301 return 0;
302 case EF_FRV_GPR32:
303 case 0:
304 break;
305 default:
306 return 0;
307 }
308
309 switch (hdr->e_flags & EF_FRV_FPR_MASK) {
310 case EF_FRV_FPR64:
311 if ((hsr0 & HSR0_FRN) == HSR0_FRN_32)
312 return 0;
313 case EF_FRV_FPR32:
314 case EF_FRV_FPR_NONE:
315 case 0:
316 break;
317 default:
318 return 0;
319 }
320
321 if ((hdr->e_flags & EF_FRV_MULADD) == EF_FRV_MULADD)
322 if (PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
323 PSR_IMPLE(psr) != PSR_IMPLE_FR451)
324 return 0;
325
326 switch (hdr->e_flags & EF_FRV_CPU_MASK) {
327 case EF_FRV_CPU_GENERIC:
328 break;
329 case EF_FRV_CPU_FR300:
330 case EF_FRV_CPU_SIMPLE:
331 case EF_FRV_CPU_TOMCAT:
332 default:
333 return 0;
334 case EF_FRV_CPU_FR400:
335 if (PSR_IMPLE(psr) != PSR_IMPLE_FR401 &&
336 PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
337 PSR_IMPLE(psr) != PSR_IMPLE_FR451 &&
338 PSR_IMPLE(psr) != PSR_IMPLE_FR551)
339 return 0;
340 break;
341 case EF_FRV_CPU_FR450:
342 if (PSR_IMPLE(psr) != PSR_IMPLE_FR451)
343 return 0;
344 break;
345 case EF_FRV_CPU_FR500:
346 if (PSR_IMPLE(psr) != PSR_IMPLE_FR501)
347 return 0;
348 break;
349 case EF_FRV_CPU_FR550:
350 if (PSR_IMPLE(psr) != PSR_IMPLE_FR551)
351 return 0;
352 break;
353 }
354
355 return 1;
356}
6d8c4e3b
DH
357
358int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
359{
360 memcpy(fpregs,
361 &current->thread.user->f,
362 sizeof(current->thread.user->f));
363 return 1;
364}