1/*
2 *  linux/arch/arm26/kernel/process.c
3 *
4 *  Copyright (C) 2003 Ian Molton - adapted for ARM26
5 *  Copyright (C) 1996-2000 Russell King - Converted to ARM.
6 *  Origional Copyright (C) 1995  Linus Torvalds
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12#include <stdarg.h>
13
14#include <linux/module.h>
15#include <linux/sched.h>
16#include <linux/kernel.h>
17#include <linux/mm.h>
18#include <linux/stddef.h>
19#include <linux/unistd.h>
20#include <linux/ptrace.h>
21#include <linux/slab.h>
22#include <linux/user.h>
23#include <linux/a.out.h>
24#include <linux/delay.h>
25#include <linux/reboot.h>
26#include <linux/interrupt.h>
27#include <linux/init.h>
28
29#include <asm/system.h>
30#include <asm/io.h>
31#include <asm/leds.h>
32#include <asm/processor.h>
33#include <asm/uaccess.h>
34
35extern const char *processor_modes[];
36extern void setup_mm_for_reboot(char mode);
37
38static volatile int hlt_counter;
39
40void disable_hlt(void)
41{
42	hlt_counter++;
43}
44
45EXPORT_SYMBOL(disable_hlt);
46
47void enable_hlt(void)
48{
49	hlt_counter--;
50}
51
52EXPORT_SYMBOL(enable_hlt);
53
54static int __init nohlt_setup(char *__unused)
55{
56	hlt_counter = 1;
57	return 1;
58}
59
60static int __init hlt_setup(char *__unused)
61{
62	hlt_counter = 0;
63	return 1;
64}
65
66__setup("nohlt", nohlt_setup);
67__setup("hlt", hlt_setup);
68
69/*
70 * This is our default idle handler.  We need to disable
71 * interrupts here to ensure we don't miss a wakeup call.
72 */
73void cpu_idle(void)
74{
75	/* endless idle loop with no priority at all */
76	while (1) {
77		while (!need_resched())
78			cpu_relax();
79		preempt_enable_no_resched();
80		schedule();
81		preempt_disable();
82	}
83}
84
85static char reboot_mode = 'h';
86
87int __init reboot_setup(char *str)
88{
89	reboot_mode = str[0];
90	return 1;
91}
92
93__setup("reboot=", reboot_setup);
94
95/* ARM26 cant do these but we still need to define them. */
96void machine_halt(void)
97{
98}
99void machine_power_off(void)
100{
101}
102
103void machine_restart(char * __unused)
104{
105	/*
106	 * Clean and disable cache, and turn off interrupts
107	 */
108	cpu_proc_fin();
109
110	/*
111	 * Tell the mm system that we are going to reboot -
112	 * we may need it to insert some 1:1 mappings so that
113	 * soft boot works.
114	 */
115	setup_mm_for_reboot(reboot_mode);
116
117	/*
118         * copy branch instruction to reset location and call it
119         */
120
121        *(unsigned long *)0 = *(unsigned long *)0x03800000;
122        ((void(*)(void))0)();
123
124	/*
125	 * Whoops - the architecture was unable to reboot.
126	 * Tell the user! Should never happen...
127	 */
128	mdelay(1000);
129	printk("Reboot failed -- System halted\n");
130	while (1);
131}
132
133void show_regs(struct pt_regs * regs)
134{
135	unsigned long flags;
136
137	flags = condition_codes(regs);
138
139	printk("pc : [<%08lx>]    lr : [<%08lx>]    %s\n"
140	       "sp : %08lx  ip : %08lx  fp : %08lx\n",
141		instruction_pointer(regs),
142		regs->ARM_lr, print_tainted(), regs->ARM_sp,
143		regs->ARM_ip, regs->ARM_fp);
144	printk("r10: %08lx  r9 : %08lx  r8 : %08lx\n",
145		regs->ARM_r10, regs->ARM_r9,
146		regs->ARM_r8);
147	printk("r7 : %08lx  r6 : %08lx  r5 : %08lx  r4 : %08lx\n",
148		regs->ARM_r7, regs->ARM_r6,
149		regs->ARM_r5, regs->ARM_r4);
150	printk("r3 : %08lx  r2 : %08lx  r1 : %08lx  r0 : %08lx\n",
151		regs->ARM_r3, regs->ARM_r2,
152		regs->ARM_r1, regs->ARM_r0);
153	printk("Flags: %c%c%c%c",
154		flags & PSR_N_BIT ? 'N' : 'n',
155		flags & PSR_Z_BIT ? 'Z' : 'z',
156		flags & PSR_C_BIT ? 'C' : 'c',
157		flags & PSR_V_BIT ? 'V' : 'v');
158	printk("  IRQs o%s  FIQs o%s  Mode %s  Segment %s\n",
159		interrupts_enabled(regs) ? "n" : "ff",
160		fast_interrupts_enabled(regs) ? "n" : "ff",
161		processor_modes[processor_mode(regs)],
162		get_fs() == get_ds() ? "kernel" : "user");
163}
164
165void show_fpregs(struct user_fp *regs)
166{
167	int i;
168
169	for (i = 0; i < 8; i++) {
170		unsigned long *p;
171		char type;
172
173		p = (unsigned long *)(regs->fpregs + i);
174
175		switch (regs->ftype[i]) {
176			case 1: type = 'f'; break;
177			case 2: type = 'd'; break;
178			case 3: type = 'e'; break;
179			default: type = '?'; break;
180		}
181		if (regs->init_flag)
182			type = '?';
183
184		printk("  f%d(%c): %08lx %08lx %08lx%c",
185			i, type, p[0], p[1], p[2], i & 1 ? '\n' : ' ');
186	}
187
188
189	printk("FPSR: %08lx FPCR: %08lx\n",
190		(unsigned long)regs->fpsr,
191		(unsigned long)regs->fpcr);
192}
193
194/*
195 * Task structure and kernel stack allocation.
196 */
197static unsigned long *thread_info_head;
198static unsigned int nr_thread_info;
199
200extern unsigned long get_page_8k(int priority);
201extern void free_page_8k(unsigned long page);
202
203#define EXTRA_TASK_STRUCT	0
204#define ll_alloc_task_struct()	((struct thread_info *)get_page_8k(GFP_KERNEL))
205#define ll_free_task_struct(p)  free_page_8k((unsigned long)(p))
206
207struct thread_info *alloc_thread_info(struct task_struct *task)
208{
209	struct thread_info *thread = NULL;
210
211	if (EXTRA_TASK_STRUCT) {
212		unsigned long *p = thread_info_head;
213
214		if (p) {
215			thread_info_head = (unsigned long *)p[0];
216			nr_thread_info -= 1;
217		}
218		thread = (struct thread_info *)p;
219	}
220
221	if (!thread)
222		thread = ll_alloc_task_struct();
223
224#ifdef CONFIG_MAGIC_SYSRQ
225	/*
226	 * The stack must be cleared if you want SYSRQ-T to
227	 * give sensible stack usage information
228	 */
229	if (thread) {
230		char *p = (char *)thread;
231		memzero(p+KERNEL_STACK_SIZE, KERNEL_STACK_SIZE);
232	}
233#endif
234	return thread;
235}
236
237void free_thread_info(struct thread_info *thread)
238{
239	if (EXTRA_TASK_STRUCT && nr_thread_info < EXTRA_TASK_STRUCT) {
240		unsigned long *p = (unsigned long *)thread;
241		p[0] = (unsigned long)thread_info_head;
242		thread_info_head = p;
243		nr_thread_info += 1;
244	} else
245		ll_free_task_struct(thread);
246}
247
248/*
249 * Free current thread data structures etc..
250 */
251void exit_thread(void)
252{
253}
254
255void flush_thread(void)
256{
257	struct thread_info *thread = current_thread_info();
258	struct task_struct *tsk = current;
259
260	memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
261	memset(&thread->fpstate, 0, sizeof(union fp_state));
262
263	clear_used_math();
264}
265
266void release_thread(struct task_struct *dead_task)
267{
268}
269
270asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
271
272int
273copy_thread(int nr, unsigned long clone_flags, unsigned long stack_start,
274	    unsigned long unused, struct task_struct *p, struct pt_regs *regs)
275{
276	struct thread_info *thread = task_thread_info(p);
277	struct pt_regs *childregs = task_pt_regs(p);
278
279	*childregs = *regs;
280	childregs->ARM_r0 = 0;
281	childregs->ARM_sp = stack_start;
282
283	memset(&thread->cpu_context, 0, sizeof(struct cpu_context_save));
284	thread->cpu_context.sp = (unsigned long)childregs;
285	thread->cpu_context.pc = (unsigned long)ret_from_fork | MODE_SVC26 | PSR_I_BIT;
286
287	return 0;
288}
289
290/*
291 * fill in the fpe structure for a core dump...
292 */
293int dump_fpu (struct pt_regs *regs, struct user_fp *fp)
294{
295	struct thread_info *thread = current_thread_info();
296	int used_math = !!used_math();
297
298	if (used_math)
299		memcpy(fp, &thread->fpstate.soft, sizeof (*fp));
300
301	return used_math;
302}
303
304/*
305 * fill in the user structure for a core dump..
306 */
307void dump_thread(struct pt_regs * regs, struct user * dump)
308{
309	struct task_struct *tsk = current;
310
311	dump->magic = CMAGIC;
312	dump->start_code = tsk->mm->start_code;
313	dump->start_stack = regs->ARM_sp & ~(PAGE_SIZE - 1);
314
315	dump->u_tsize = (tsk->mm->end_code - tsk->mm->start_code) >> PAGE_SHIFT;
316	dump->u_dsize = (tsk->mm->brk - tsk->mm->start_data + PAGE_SIZE - 1) >> PAGE_SHIFT;
317	dump->u_ssize = 0;
318
319	dump->u_debugreg[0] = tsk->thread.debug.bp[0].address;
320	dump->u_debugreg[1] = tsk->thread.debug.bp[1].address;
321	dump->u_debugreg[2] = tsk->thread.debug.bp[0].insn;
322	dump->u_debugreg[3] = tsk->thread.debug.bp[1].insn;
323	dump->u_debugreg[4] = tsk->thread.debug.nsaved;
324
325	if (dump->start_stack < 0x04000000)
326		dump->u_ssize = (0x04000000 - dump->start_stack) >> PAGE_SHIFT;
327
328	dump->regs = *regs;
329	dump->u_fpvalid = dump_fpu (regs, &dump->u_fp);
330}
331
332extern void kernel_thread_helper(void);
333
334asm(    ".section .text\n"
335"       .align\n"
336"       .type   kernel_thread_helper, #function\n"
337"kernel_thread_helper:\n"
338"       mov     r0, r1\n"
339"       mov     lr, r3\n"
340"       mov     pc, r2\n"
341"       .size   kernel_thread_helper, . - kernel_thread_helper\n"
342"       .previous");
343
344/*
345 * Create a kernel thread.
346 */
347pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
348{
349        struct pt_regs regs;
350
351        memset(&regs, 0, sizeof(regs));
352
353        regs.ARM_r1 = (unsigned long)arg;
354        regs.ARM_r2 = (unsigned long)fn;
355        regs.ARM_r3 = (unsigned long)do_exit;
356        regs.ARM_pc = (unsigned long)kernel_thread_helper | MODE_SVC26;
357
358        return do_fork(flags|CLONE_VM|CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
359}
360EXPORT_SYMBOL(kernel_thread);
361
362
363unsigned long get_wchan(struct task_struct *p)
364{
365	unsigned long fp, lr;
366	unsigned long stack_page;
367	int count = 0;
368	if (!p || p == current || p->state == TASK_RUNNING)
369		return 0;
370
371	stack_page = 4096 + (unsigned long)p;
372	fp = thread_saved_fp(p);
373	do {
374		if (fp < stack_page || fp > 4092+stack_page)
375			return 0;
376		lr = pc_pointer (((unsigned long *)fp)[-1]);
377		if (!in_sched_functions(lr))
378			return lr;
379		fp = *(unsigned long *) (fp - 12);
380	} while (count ++ < 16);
381	return 0;
382}
383