blob: 14707f25153b15f46e97a4ad68ca5dc521bc1984 [file] [log] [blame]
David Howellsb920de12008-02-08 04:19:31 -08001/* 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>
David Howellsb920de12008-02-08 04:19:31 -080017#include <linux/stddef.h>
18#include <linux/unistd.h>
19#include <linux/ptrace.h>
David Howellsb920de12008-02-08 04:19:31 -080020#include <linux/user.h>
David Howellsb920de12008-02-08 04:19:31 -080021#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
David Howellsb920de12008-02-08 04:19:31 -080028#include <asm/uaccess.h>
29#include <asm/pgtable.h>
David Howellsb920de12008-02-08 04:19:31 -080030#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 */
41void (*pm_idle)(void);
42EXPORT_SYMBOL(pm_idle);
43
44/*
45 * return saved PC of a blocked thread.
46 */
47unsigned 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 */
55void (*pm_power_off)(void);
56EXPORT_SYMBOL(pm_power_off);
57
Akira Takeuchi368dd5a2010-10-27 17:28:55 +010058#if !defined(CONFIG_SMP) || defined(CONFIG_HOTPLUG_CPU)
David Howellsb920de12008-02-08 04:19:31 -080059/*
60 * we use this if we don't have any better idle routine
61 */
62static void default_idle(void)
63{
64 local_irq_disable();
65 if (!need_resched())
66 safe_halt();
67 else
68 local_irq_enable();
69}
70
Akira Takeuchi368dd5a2010-10-27 17:28:55 +010071#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 */
77static 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
David Howellsb920de12008-02-08 04:19:31 -0800100/*
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 */
106void cpu_idle(void)
107{
David Howellsb920de12008-02-08 04:19:31 -0800108 /* 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;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100115 if (!idle) {
116#if defined(CONFIG_SMP) && !defined(CONFIG_HOTPLUG_CPU)
117 idle = poll_idle;
118#else /* CONFIG_SMP && !CONFIG_HOTPLUG_CPU */
David Howellsb920de12008-02-08 04:19:31 -0800119 idle = default_idle;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100120#endif /* CONFIG_SMP && !CONFIG_HOTPLUG_CPU */
121 }
David Howellsb920de12008-02-08 04:19:31 -0800122 idle();
123 }
124
Thomas Gleixnerbd2f5532011-03-21 12:33:18 +0100125 schedule_preempt_disabled();
David Howellsb920de12008-02-08 04:19:31 -0800126 }
127}
128
129void release_segments(struct mm_struct *mm)
130{
131}
132
133void machine_restart(char *cmd)
134{
David Howells044264b2011-03-18 16:54:31 +0000135#ifdef CONFIG_KERNEL_DEBUGGER
David Howellsb920de12008-02-08 04:19:31 -0800136 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
146void machine_halt(void)
147{
David Howells044264b2011-03-18 16:54:31 +0000148#ifdef CONFIG_KERNEL_DEBUGGER
David Howellsb920de12008-02-08 04:19:31 -0800149 gdbstub_exit(0);
150#endif
151}
152
153void machine_power_off(void)
154{
David Howells044264b2011-03-18 16:54:31 +0000155#ifdef CONFIG_KERNEL_DEBUGGER
David Howellsb920de12008-02-08 04:19:31 -0800156 gdbstub_exit(0);
157#endif
158}
159
160void show_regs(struct pt_regs *regs)
161{
162}
163
164/*
165 * create a kernel thread
166 */
167int 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}
David Howells7fc72282008-07-04 09:59:46 -0700183EXPORT_SYMBOL(kernel_thread);
David Howellsb920de12008-02-08 04:19:31 -0800184
185/*
186 * free current thread data structures etc..
187 */
188void exit_thread(void)
189{
190 exit_fpu();
191}
192
193void flush_thread(void)
194{
195 flush_fpu();
196}
197
198void 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 */
206void copy_segments(struct task_struct *p, struct mm_struct *new_mm)
207{
208}
209
210/*
211 * this gets called before we allocate a new thread and copy the current task
212 * into it so that we can store lazy state into memory
213 */
214void prepare_to_copy(struct task_struct *tsk)
215{
216 unlazy_fpu(tsk);
217}
218
219/*
220 * set up the kernel stack for a new thread and copy arch-specific thread
221 * control information
222 */
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700223int copy_thread(unsigned long clone_flags,
David Howellsb920de12008-02-08 04:19:31 -0800224 unsigned long c_usp, unsigned long ustk_size,
225 struct task_struct *p, struct pt_regs *kregs)
226{
David Howells7c7fcf72010-10-27 17:29:01 +0100227 struct thread_info *ti = task_thread_info(p);
David Howellsb920de12008-02-08 04:19:31 -0800228 struct pt_regs *c_uregs, *c_kregs, *uregs;
229 unsigned long c_ksp;
230
231 uregs = current->thread.uregs;
232
233 c_ksp = (unsigned long) task_stack_page(p) + THREAD_SIZE;
234
235 /* allocate the userspace exception frame and set it up */
236 c_ksp -= sizeof(struct pt_regs);
237 c_uregs = (struct pt_regs *) c_ksp;
238
239 p->thread.uregs = c_uregs;
240 *c_uregs = *uregs;
241 c_uregs->sp = c_usp;
242 c_uregs->epsw &= ~EPSW_FE; /* my FPU */
243
244 c_ksp -= 12; /* allocate function call ABI slack */
245
246 /* the new TLS pointer is passed in as arg #5 to sys_clone() */
247 if (clone_flags & CLONE_SETTLS)
David Howells7c7fcf72010-10-27 17:29:01 +0100248 c_uregs->e2 = current_frame()->d3;
David Howellsb920de12008-02-08 04:19:31 -0800249
250 /* set up the return kernel frame if called from kernel_thread() */
251 c_kregs = c_uregs;
252 if (kregs != uregs) {
253 c_ksp -= sizeof(struct pt_regs);
254 c_kregs = (struct pt_regs *) c_ksp;
255 *c_kregs = *kregs;
256 c_kregs->sp = c_usp;
257 c_kregs->next = c_uregs;
258#ifdef CONFIG_MN10300_CURRENT_IN_E2
259 c_kregs->e2 = (unsigned long) p; /* current */
260#endif
261
262 c_ksp -= 12; /* allocate function call ABI slack */
263 }
264
265 /* set up things up so the scheduler can start the new task */
David Howells7c7fcf72010-10-27 17:29:01 +0100266 ti->frame = c_kregs;
David Howellsb920de12008-02-08 04:19:31 -0800267 p->thread.a3 = (unsigned long) c_kregs;
268 p->thread.sp = c_ksp;
269 p->thread.pc = (unsigned long) ret_from_fork;
270 p->thread.wchan = (unsigned long) ret_from_fork;
271 p->thread.usp = c_usp;
272
273 return 0;
274}
275
276/*
277 * clone a process
David Howells7c7fcf72010-10-27 17:29:01 +0100278 * - tlsptr is retrieved by copy_thread() from current_frame()->d3
David Howellsb920de12008-02-08 04:19:31 -0800279 */
280asmlinkage long sys_clone(unsigned long clone_flags, unsigned long newsp,
281 int __user *parent_tidptr, int __user *child_tidptr,
282 int __user *tlsptr)
283{
David Howells7c7fcf72010-10-27 17:29:01 +0100284 return do_fork(clone_flags, newsp ?: current_frame()->sp,
285 current_frame(), 0, parent_tidptr, child_tidptr);
David Howellsb920de12008-02-08 04:19:31 -0800286}
287
288asmlinkage long sys_fork(void)
289{
David Howells7c7fcf72010-10-27 17:29:01 +0100290 return do_fork(SIGCHLD, current_frame()->sp,
291 current_frame(), 0, NULL, NULL);
David Howellsb920de12008-02-08 04:19:31 -0800292}
293
294asmlinkage long sys_vfork(void)
295{
David Howells7c7fcf72010-10-27 17:29:01 +0100296 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, current_frame()->sp,
297 current_frame(), 0, NULL, NULL);
David Howellsb920de12008-02-08 04:19:31 -0800298}
299
David Howellsc7887322010-08-11 11:26:22 +0100300asmlinkage long sys_execve(const char __user *name,
David Howellsd7627462010-08-17 23:52:56 +0100301 const char __user *const __user *argv,
302 const char __user *const __user *envp)
David Howellsb920de12008-02-08 04:19:31 -0800303{
304 char *filename;
305 int error;
306
David Howellsb920de12008-02-08 04:19:31 -0800307 filename = getname(name);
308 error = PTR_ERR(filename);
John Kacur8c0daee2009-10-12 23:41:55 +0200309 if (IS_ERR(filename))
310 return error;
David Howells7c7fcf72010-10-27 17:29:01 +0100311 error = do_execve(filename, argv, envp, current_frame());
John Kacur8c0daee2009-10-12 23:41:55 +0200312 putname(filename);
David Howellsb920de12008-02-08 04:19:31 -0800313 return error;
314}
315
316unsigned long get_wchan(struct task_struct *p)
317{
318 return p->thread.wchan;
319}