fork: move the real prepare_to_copy() users to arch_dup_task_struct()
[pandora-kernel.git] / arch / mn10300 / kernel / process.c
1 /* MN10300  Process handling code
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  */
11 #include <linux/module.h>
12 #include <linux/errno.h>
13 #include <linux/sched.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/stddef.h>
18 #include <linux/unistd.h>
19 #include <linux/ptrace.h>
20 #include <linux/user.h>
21 #include <linux/interrupt.h>
22 #include <linux/delay.h>
23 #include <linux/reboot.h>
24 #include <linux/percpu.h>
25 #include <linux/err.h>
26 #include <linux/fs.h>
27 #include <linux/slab.h>
28 #include <asm/uaccess.h>
29 #include <asm/pgtable.h>
30 #include <asm/io.h>
31 #include <asm/processor.h>
32 #include <asm/mmu_context.h>
33 #include <asm/fpu.h>
34 #include <asm/reset-regs.h>
35 #include <asm/gdb-stub.h>
36 #include "internal.h"
37
38 /*
39  * power management idle function, if any..
40  */
41 void (*pm_idle)(void);
42 EXPORT_SYMBOL(pm_idle);
43
44 /*
45  * return saved PC of a blocked thread.
46  */
47 unsigned long thread_saved_pc(struct task_struct *tsk)
48 {
49         return ((unsigned long *) tsk->thread.sp)[3];
50 }
51
52 /*
53  * power off function, if any
54  */
55 void (*pm_power_off)(void);
56 EXPORT_SYMBOL(pm_power_off);
57
58 #if !defined(CONFIG_SMP) || defined(CONFIG_HOTPLUG_CPU)
59 /*
60  * we use this if we don't have any better idle routine
61  */
62 static void default_idle(void)
63 {
64         local_irq_disable();
65         if (!need_resched())
66                 safe_halt();
67         else
68                 local_irq_enable();
69 }
70
71 #else /* !CONFIG_SMP || CONFIG_HOTPLUG_CPU  */
72 /*
73  * On SMP it's slightly faster (but much more power-consuming!)
74  * to poll the ->work.need_resched flag instead of waiting for the
75  * cross-CPU IPI to arrive. Use this option with caution.
76  */
77 static inline void poll_idle(void)
78 {
79         int oldval;
80
81         local_irq_enable();
82
83         /*
84          * Deal with another CPU just having chosen a thread to
85          * run here:
86          */
87         oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
88
89         if (!oldval) {
90                 set_thread_flag(TIF_POLLING_NRFLAG);
91                 while (!need_resched())
92                         cpu_relax();
93                 clear_thread_flag(TIF_POLLING_NRFLAG);
94         } else {
95                 set_need_resched();
96         }
97 }
98 #endif /* !CONFIG_SMP || CONFIG_HOTPLUG_CPU */
99
100 /*
101  * the idle thread
102  * - there's no useful work to be done, so just try to conserve power and have
103  *   a low exit latency (ie sit in a loop waiting for somebody to say that
104  *   they'd like to reschedule)
105  */
106 void cpu_idle(void)
107 {
108         /* endless idle loop with no priority at all */
109         for (;;) {
110                 while (!need_resched()) {
111                         void (*idle)(void);
112
113                         smp_rmb();
114                         idle = pm_idle;
115                         if (!idle) {
116 #if defined(CONFIG_SMP) && !defined(CONFIG_HOTPLUG_CPU)
117                                 idle = poll_idle;
118 #else  /* CONFIG_SMP && !CONFIG_HOTPLUG_CPU */
119                                 idle = default_idle;
120 #endif /* CONFIG_SMP && !CONFIG_HOTPLUG_CPU */
121                         }
122                         idle();
123                 }
124
125                 schedule_preempt_disabled();
126         }
127 }
128
129 void release_segments(struct mm_struct *mm)
130 {
131 }
132
133 void machine_restart(char *cmd)
134 {
135 #ifdef CONFIG_KERNEL_DEBUGGER
136         gdbstub_exit(0);
137 #endif
138
139 #ifdef mn10300_unit_hard_reset
140         mn10300_unit_hard_reset();
141 #else
142         mn10300_proc_hard_reset();
143 #endif
144 }
145
146 void machine_halt(void)
147 {
148 #ifdef CONFIG_KERNEL_DEBUGGER
149         gdbstub_exit(0);
150 #endif
151 }
152
153 void machine_power_off(void)
154 {
155 #ifdef CONFIG_KERNEL_DEBUGGER
156         gdbstub_exit(0);
157 #endif
158 }
159
160 void show_regs(struct pt_regs *regs)
161 {
162 }
163
164 /*
165  * create a kernel thread
166  */
167 int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
168 {
169         struct pt_regs regs;
170
171         memset(&regs, 0, sizeof(regs));
172
173         regs.a2 = (unsigned long) fn;
174         regs.d2 = (unsigned long) arg;
175         regs.pc = (unsigned long) kernel_thread_helper;
176         local_save_flags(regs.epsw);
177         regs.epsw |= EPSW_IE | EPSW_IM_7;
178
179         /* Ok, create the new process.. */
180         return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0,
181                        NULL, NULL);
182 }
183 EXPORT_SYMBOL(kernel_thread);
184
185 /*
186  * free current thread data structures etc..
187  */
188 void exit_thread(void)
189 {
190         exit_fpu();
191 }
192
193 void flush_thread(void)
194 {
195         flush_fpu();
196 }
197
198 void release_thread(struct task_struct *dead_task)
199 {
200 }
201
202 /*
203  * we do not have to muck with descriptors here, that is
204  * done in switch_mm() as needed.
205  */
206 void copy_segments(struct task_struct *p, struct mm_struct *new_mm)
207 {
208 }
209
210 /*
211  * this gets called so that we can store lazy state into memory and copy the
212  * current task into the new thread.
213  */
214 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
215 {
216         unlazy_fpu(src);
217         *dst = *src;
218         return 0;
219 }
220
221 /*
222  * set up the kernel stack for a new thread and copy arch-specific thread
223  * control information
224  */
225 int copy_thread(unsigned long clone_flags,
226                 unsigned long c_usp, unsigned long ustk_size,
227                 struct task_struct *p, struct pt_regs *kregs)
228 {
229         struct thread_info *ti = task_thread_info(p);
230         struct pt_regs *c_uregs, *c_kregs, *uregs;
231         unsigned long c_ksp;
232
233         uregs = current->thread.uregs;
234
235         c_ksp = (unsigned long) task_stack_page(p) + THREAD_SIZE;
236
237         /* allocate the userspace exception frame and set it up */
238         c_ksp -= sizeof(struct pt_regs);
239         c_uregs = (struct pt_regs *) c_ksp;
240
241         p->thread.uregs = c_uregs;
242         *c_uregs = *uregs;
243         c_uregs->sp = c_usp;
244         c_uregs->epsw &= ~EPSW_FE; /* my FPU */
245
246         c_ksp -= 12; /* allocate function call ABI slack */
247
248         /* the new TLS pointer is passed in as arg #5 to sys_clone() */
249         if (clone_flags & CLONE_SETTLS)
250                 c_uregs->e2 = current_frame()->d3;
251
252         /* set up the return kernel frame if called from kernel_thread() */
253         c_kregs = c_uregs;
254         if (kregs != uregs) {
255                 c_ksp -= sizeof(struct pt_regs);
256                 c_kregs = (struct pt_regs *) c_ksp;
257                 *c_kregs = *kregs;
258                 c_kregs->sp = c_usp;
259                 c_kregs->next = c_uregs;
260 #ifdef CONFIG_MN10300_CURRENT_IN_E2
261                 c_kregs->e2 = (unsigned long) p; /* current */
262 #endif
263
264                 c_ksp -= 12; /* allocate function call ABI slack */
265         }
266
267         /* set up things up so the scheduler can start the new task */
268         ti->frame       = c_kregs;
269         p->thread.a3    = (unsigned long) c_kregs;
270         p->thread.sp    = c_ksp;
271         p->thread.pc    = (unsigned long) ret_from_fork;
272         p->thread.wchan = (unsigned long) ret_from_fork;
273         p->thread.usp   = c_usp;
274
275         return 0;
276 }
277
278 /*
279  * clone a process
280  * - tlsptr is retrieved by copy_thread() from current_frame()->d3
281  */
282 asmlinkage long sys_clone(unsigned long clone_flags, unsigned long newsp,
283                           int __user *parent_tidptr, int __user *child_tidptr,
284                           int __user *tlsptr)
285 {
286         return do_fork(clone_flags, newsp ?: current_frame()->sp,
287                        current_frame(), 0, parent_tidptr, child_tidptr);
288 }
289
290 asmlinkage long sys_fork(void)
291 {
292         return do_fork(SIGCHLD, current_frame()->sp,
293                        current_frame(), 0, NULL, NULL);
294 }
295
296 asmlinkage long sys_vfork(void)
297 {
298         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, current_frame()->sp,
299                        current_frame(), 0, NULL, NULL);
300 }
301
302 asmlinkage long sys_execve(const char __user *name,
303                            const char __user *const __user *argv,
304                            const char __user *const __user *envp)
305 {
306         char *filename;
307         int error;
308
309         filename = getname(name);
310         error = PTR_ERR(filename);
311         if (IS_ERR(filename))
312                 return error;
313         error = do_execve(filename, argv, envp, current_frame());
314         putname(filename);
315         return error;
316 }
317
318 unsigned long get_wchan(struct task_struct *p)
319 {
320         return p->thread.wchan;
321 }