1/*
2 *  linux/arch/m32r/kernel/process.c
3 *
4 *  Copyright (c) 2001, 2002  Hiroyuki Kondo, Hirokazu Takata,
5 *                            Hitoshi Yamamoto
6 *  Taken from sh version.
7 *    Copyright (C) 1995  Linus Torvalds
8 *    SuperH version:  Copyright (C) 1999, 2000  Niibe Yutaka & Kaz Kojima
9 */
10
11#undef DEBUG_PROCESS
12#ifdef DEBUG_PROCESS
13#define DPRINTK(fmt, args...)  printk("%s:%d:%s: " fmt, __FILE__, __LINE__, \
14  __FUNCTION__, ##args)
15#else
16#define DPRINTK(fmt, args...)
17#endif
18
19/*
20 * This file handles the architecture-dependent parts of process handling..
21 */
22
23#include <linux/fs.h>
24#include <linux/module.h>
25#include <linux/ptrace.h>
26#include <linux/unistd.h>
27#include <linux/slab.h>
28#include <linux/hardirq.h>
29
30#include <asm/io.h>
31#include <asm/uaccess.h>
32#include <asm/mmu_context.h>
33#include <asm/elf.h>
34#include <asm/m32r.h>
35
36#include <linux/err.h>
37
38static int hlt_counter=0;
39
40/*
41 * Return saved PC of a blocked thread.
42 */
43unsigned long thread_saved_pc(struct task_struct *tsk)
44{
45	return tsk->thread.lr;
46}
47
48/*
49 * Powermanagement idle function, if any..
50 */
51void (*pm_idle)(void) = NULL;
52EXPORT_SYMBOL(pm_idle);
53
54void (*pm_power_off)(void) = NULL;
55EXPORT_SYMBOL(pm_power_off);
56
57void disable_hlt(void)
58{
59	hlt_counter++;
60}
61
62EXPORT_SYMBOL(disable_hlt);
63
64void enable_hlt(void)
65{
66	hlt_counter--;
67}
68
69EXPORT_SYMBOL(enable_hlt);
70
71/*
72 * We use this is we don't have any better
73 * idle routine..
74 */
75void default_idle(void)
76{
77	/* M32R_FIXME: Please use "cpu_sleep" mode.  */
78	cpu_relax();
79}
80
81/*
82 * On SMP it's slightly faster (but much more power-consuming!)
83 * to poll the ->work.need_resched flag instead of waiting for the
84 * cross-CPU IPI to arrive. Use this option with caution.
85 */
86static void poll_idle (void)
87{
88	/* M32R_FIXME */
89	cpu_relax();
90}
91
92/*
93 * The idle thread. There's no useful work to be
94 * done, so just try to conserve power and have a
95 * low exit latency (ie sit in a loop waiting for
96 * somebody to say that they'd like to reschedule)
97 */
98void cpu_idle (void)
99{
100	/* endless idle loop with no priority at all */
101	while (1) {
102		while (!need_resched()) {
103			void (*idle)(void) = pm_idle;
104
105			if (!idle)
106				idle = default_idle;
107
108			idle();
109		}
110		preempt_enable_no_resched();
111		schedule();
112		preempt_disable();
113	}
114}
115
116void machine_restart(char *__unused)
117{
118#if defined(CONFIG_PLAT_MAPPI3)
119	outw(1, (unsigned long)PLD_REBOOT);
120#endif
121
122	printk("Please push reset button!\n");
123	while (1)
124		cpu_relax();
125}
126
127void machine_halt(void)
128{
129	printk("Please push reset button!\n");
130	while (1)
131		cpu_relax();
132}
133
134void machine_power_off(void)
135{
136	/* M32R_FIXME */
137}
138
139static int __init idle_setup (char *str)
140{
141	if (!strncmp(str, "poll", 4)) {
142		printk("using poll in idle threads.\n");
143		pm_idle = poll_idle;
144	} else if (!strncmp(str, "sleep", 4)) {
145		printk("using sleep in idle threads.\n");
146		pm_idle = default_idle;
147	}
148
149	return 1;
150}
151
152__setup("idle=", idle_setup);
153
154void show_regs(struct pt_regs * regs)
155{
156	printk("\n");
157	printk("BPC[%08lx]:PSW[%08lx]:LR [%08lx]:FP [%08lx]\n", \
158	  regs->bpc, regs->psw, regs->lr, regs->fp);
159	printk("BBPC[%08lx]:BBPSW[%08lx]:SPU[%08lx]:SPI[%08lx]\n", \
160	  regs->bbpc, regs->bbpsw, regs->spu, regs->spi);
161	printk("R0 [%08lx]:R1 [%08lx]:R2 [%08lx]:R3 [%08lx]\n", \
162	  regs->r0, regs->r1, regs->r2, regs->r3);
163	printk("R4 [%08lx]:R5 [%08lx]:R6 [%08lx]:R7 [%08lx]\n", \
164	  regs->r4, regs->r5, regs->r6, regs->r7);
165	printk("R8 [%08lx]:R9 [%08lx]:R10[%08lx]:R11[%08lx]\n", \
166	  regs->r8, regs->r9, regs->r10, regs->r11);
167	printk("R12[%08lx]\n", \
168	  regs->r12);
169
170#if defined(CONFIG_ISA_M32R2) && defined(CONFIG_ISA_DSP_LEVEL2)
171	printk("ACC0H[%08lx]:ACC0L[%08lx]\n", \
172	  regs->acc0h, regs->acc0l);
173	printk("ACC1H[%08lx]:ACC1L[%08lx]\n", \
174	  regs->acc1h, regs->acc1l);
175#elif defined(CONFIG_ISA_M32R2) || defined(CONFIG_ISA_M32R)
176	printk("ACCH[%08lx]:ACCL[%08lx]\n", \
177	  regs->acc0h, regs->acc0l);
178#else
179#error unknown isa configuration
180#endif
181}
182
183/*
184 * Create a kernel thread
185 */
186
187/*
188 * This is the mechanism for creating a new kernel thread.
189 *
190 * NOTE! Only a kernel-only process(ie the swapper or direct descendants
191 * who haven't done an "execve()") should use this: it will work within
192 * a system call from a "real" process, but the process memory space will
193 * not be free'd until both the parent and the child have exited.
194 */
195static void kernel_thread_helper(void *nouse, int (*fn)(void *), void *arg)
196{
197	fn(arg);
198	do_exit(-1);
199}
200
201int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
202{
203	struct pt_regs regs;
204
205	memset(&regs, 0, sizeof (regs));
206	regs.r1 = (unsigned long)fn;
207	regs.r2 = (unsigned long)arg;
208
209	regs.bpc = (unsigned long)kernel_thread_helper;
210
211	regs.psw = M32R_PSW_BIE;
212
213	/* Ok, create the new process. */
214	return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL,
215		NULL);
216}
217
218/*
219 * Free current thread data structures etc..
220 */
221void exit_thread(void)
222{
223	/* Nothing to do. */
224	DPRINTK("pid = %d\n", current->pid);
225}
226
227void flush_thread(void)
228{
229	DPRINTK("pid = %d\n", current->pid);
230	memset(&current->thread.debug_trap, 0, sizeof(struct debug_trap));
231}
232
233void release_thread(struct task_struct *dead_task)
234{
235	/* do nothing */
236	DPRINTK("pid = %d\n", dead_task->pid);
237}
238
239/* Fill in the fpu structure for a core dump.. */
240int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
241{
242	return 0; /* Task didn't use the fpu at all. */
243}
244
245int copy_thread(int nr, unsigned long clone_flags, unsigned long spu,
246	unsigned long unused, struct task_struct *tsk, struct pt_regs *regs)
247{
248	struct pt_regs *childregs = task_pt_regs(tsk);
249	extern void ret_from_fork(void);
250
251	/* Copy registers */
252	*childregs = *regs;
253
254	childregs->spu = spu;
255	childregs->r0 = 0;	/* Child gets zero as return value */
256	regs->r0 = tsk->pid;
257	tsk->thread.sp = (unsigned long)childregs;
258	tsk->thread.lr = (unsigned long)ret_from_fork;
259
260	return 0;
261}
262
263/*
264 * Capture the user space registers if the task is not running (in user space)
265 */
266int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
267{
268	/* M32R_FIXME */
269	return 1;
270}
271
272asmlinkage int sys_fork(unsigned long r0, unsigned long r1, unsigned long r2,
273	unsigned long r3, unsigned long r4, unsigned long r5, unsigned long r6,
274	struct pt_regs regs)
275{
276#ifdef CONFIG_MMU
277	return do_fork(SIGCHLD, regs.spu, &regs, 0, NULL, NULL);
278#else
279	return -EINVAL;
280#endif /* CONFIG_MMU */
281}
282
283asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
284			 unsigned long parent_tidptr,
285			 unsigned long child_tidptr,
286			 unsigned long r4, unsigned long r5, unsigned long r6,
287			 struct pt_regs regs)
288{
289	if (!newsp)
290		newsp = regs.spu;
291
292	return do_fork(clone_flags, newsp, &regs, 0,
293		       (int __user *)parent_tidptr, (int __user *)child_tidptr);
294}
295
296/*
297 * This is trivial, and on the face of it looks like it
298 * could equally well be done in user mode.
299 *
300 * Not so, for quite unobvious reasons - register pressure.
301 * In user mode vfork() cannot have a stack frame, and if
302 * done by calling the "clone()" system call directly, you
303 * do not have enough call-clobbered registers to hold all
304 * the information you need.
305 */
306asmlinkage int sys_vfork(unsigned long r0, unsigned long r1, unsigned long r2,
307	unsigned long r3, unsigned long r4, unsigned long r5, unsigned long r6,
308	struct pt_regs regs)
309{
310	return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.spu, &regs, 0,
311			NULL, NULL);
312}
313
314/*
315 * sys_execve() executes a new program.
316 */
317asmlinkage int sys_execve(char __user *ufilename, char __user * __user *uargv,
318			  char __user * __user *uenvp,
319			  unsigned long r3, unsigned long r4, unsigned long r5,
320			  unsigned long r6, struct pt_regs regs)
321{
322	int error;
323	char *filename;
324
325	filename = getname(ufilename);
326	error = PTR_ERR(filename);
327	if (IS_ERR(filename))
328		goto out;
329
330	error = do_execve(filename, uargv, uenvp, &regs);
331	if (error == 0) {
332		task_lock(current);
333		current->ptrace &= ~PT_DTRACE;
334		task_unlock(current);
335	}
336	putname(filename);
337out:
338	return error;
339}
340
341/*
342 * These bracket the sleeping functions..
343 */
344#define first_sched	((unsigned long) scheduling_functions_start_here)
345#define last_sched	((unsigned long) scheduling_functions_end_here)
346
347unsigned long get_wchan(struct task_struct *p)
348{
349	/* M32R_FIXME */
350	return (0);
351}
352