1 /*
2  * Copyright (C) 2004-2006 Atmel Corporation
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 #include <linux/sched.h>
9 #include <linux/module.h>
10 #include <linux/kallsyms.h>
11 #include <linux/fs.h>
12 #include <linux/pm.h>
13 #include <linux/ptrace.h>
14 #include <linux/slab.h>
15 #include <linux/reboot.h>
16 #include <linux/tick.h>
17 #include <linux/uaccess.h>
18 #include <linux/unistd.h>
19 
20 #include <asm/sysreg.h>
21 #include <asm/ocd.h>
22 #include <asm/syscalls.h>
23 
24 #include <mach/pm.h>
25 
26 void (*pm_power_off)(void);
27 EXPORT_SYMBOL(pm_power_off);
28 
29 /*
30  * This file handles the architecture-dependent parts of process handling..
31  */
32 
cpu_idle(void)33 void cpu_idle(void)
34 {
35 	/* endless idle loop with no priority at all */
36 	while (1) {
37 		tick_nohz_idle_enter();
38 		rcu_idle_enter();
39 		while (!need_resched())
40 			cpu_idle_sleep();
41 		rcu_idle_exit();
42 		tick_nohz_idle_exit();
43 		preempt_enable_no_resched();
44 		schedule();
45 		preempt_disable();
46 	}
47 }
48 
machine_halt(void)49 void machine_halt(void)
50 {
51 	/*
52 	 * Enter Stop mode. The 32 kHz oscillator will keep running so
53 	 * the RTC will keep the time properly and the system will
54 	 * boot quickly.
55 	 */
56 	asm volatile("sleep 3\n\t"
57 		     "sub pc, -2");
58 }
59 
machine_power_off(void)60 void machine_power_off(void)
61 {
62 	if (pm_power_off)
63 		pm_power_off();
64 }
65 
machine_restart(char * cmd)66 void machine_restart(char *cmd)
67 {
68 	ocd_write(DC, (1 << OCD_DC_DBE_BIT));
69 	ocd_write(DC, (1 << OCD_DC_RES_BIT));
70 	while (1) ;
71 }
72 
73 /*
74  * PC is actually discarded when returning from a system call -- the
75  * return address must be stored in LR. This function will make sure
76  * LR points to do_exit before starting the thread.
77  *
78  * Also, when returning from fork(), r12 is 0, so we must copy the
79  * argument as well.
80  *
81  *  r0 : The argument to the main thread function
82  *  r1 : The address of do_exit
83  *  r2 : The address of the main thread function
84  */
85 asmlinkage extern void kernel_thread_helper(void);
86 __asm__("	.type	kernel_thread_helper, @function\n"
87 	"kernel_thread_helper:\n"
88 	"	mov	r12, r0\n"
89 	"	mov	lr, r2\n"
90 	"	mov	pc, r1\n"
91 	"	.size	kernel_thread_helper, . - kernel_thread_helper");
92 
kernel_thread(int (* fn)(void *),void * arg,unsigned long flags)93 int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
94 {
95 	struct pt_regs regs;
96 
97 	memset(&regs, 0, sizeof(regs));
98 
99 	regs.r0 = (unsigned long)arg;
100 	regs.r1 = (unsigned long)fn;
101 	regs.r2 = (unsigned long)do_exit;
102 	regs.lr = (unsigned long)kernel_thread_helper;
103 	regs.pc = (unsigned long)kernel_thread_helper;
104 	regs.sr = MODE_SUPERVISOR;
105 
106 	return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
107 		       0, &regs, 0, NULL, NULL);
108 }
109 EXPORT_SYMBOL(kernel_thread);
110 
111 /*
112  * Free current thread data structures etc
113  */
exit_thread(void)114 void exit_thread(void)
115 {
116 	ocd_disable(current);
117 }
118 
flush_thread(void)119 void flush_thread(void)
120 {
121 	/* nothing to do */
122 }
123 
release_thread(struct task_struct * dead_task)124 void release_thread(struct task_struct *dead_task)
125 {
126 	/* do nothing */
127 }
128 
dump_mem(const char * str,const char * log_lvl,unsigned long bottom,unsigned long top)129 static void dump_mem(const char *str, const char *log_lvl,
130 		     unsigned long bottom, unsigned long top)
131 {
132 	unsigned long p;
133 	int i;
134 
135 	printk("%s%s(0x%08lx to 0x%08lx)\n", log_lvl, str, bottom, top);
136 
137 	for (p = bottom & ~31; p < top; ) {
138 		printk("%s%04lx: ", log_lvl, p & 0xffff);
139 
140 		for (i = 0; i < 8; i++, p += 4) {
141 			unsigned int val;
142 
143 			if (p < bottom || p >= top)
144 				printk("         ");
145 			else {
146 				if (__get_user(val, (unsigned int __user *)p)) {
147 					printk("\n");
148 					goto out;
149 				}
150 				printk("%08x ", val);
151 			}
152 		}
153 		printk("\n");
154 	}
155 
156 out:
157 	return;
158 }
159 
valid_stack_ptr(struct thread_info * tinfo,unsigned long p)160 static inline int valid_stack_ptr(struct thread_info *tinfo, unsigned long p)
161 {
162 	return (p > (unsigned long)tinfo)
163 		&& (p < (unsigned long)tinfo + THREAD_SIZE - 3);
164 }
165 
166 #ifdef CONFIG_FRAME_POINTER
show_trace_log_lvl(struct task_struct * tsk,unsigned long * sp,struct pt_regs * regs,const char * log_lvl)167 static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp,
168 			       struct pt_regs *regs, const char *log_lvl)
169 {
170 	unsigned long lr, fp;
171 	struct thread_info *tinfo;
172 
173 	if (regs)
174 		fp = regs->r7;
175 	else if (tsk == current)
176 		asm("mov %0, r7" : "=r"(fp));
177 	else
178 		fp = tsk->thread.cpu_context.r7;
179 
180 	/*
181 	 * Walk the stack as long as the frame pointer (a) is within
182 	 * the kernel stack of the task, and (b) it doesn't move
183 	 * downwards.
184 	 */
185 	tinfo = task_thread_info(tsk);
186 	printk("%sCall trace:\n", log_lvl);
187 	while (valid_stack_ptr(tinfo, fp)) {
188 		unsigned long new_fp;
189 
190 		lr = *(unsigned long *)fp;
191 #ifdef CONFIG_KALLSYMS
192 		printk("%s [<%08lx>] ", log_lvl, lr);
193 #else
194 		printk(" [<%08lx>] ", lr);
195 #endif
196 		print_symbol("%s\n", lr);
197 
198 		new_fp = *(unsigned long *)(fp + 4);
199 		if (new_fp <= fp)
200 			break;
201 		fp = new_fp;
202 	}
203 	printk("\n");
204 }
205 #else
show_trace_log_lvl(struct task_struct * tsk,unsigned long * sp,struct pt_regs * regs,const char * log_lvl)206 static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp,
207 			       struct pt_regs *regs, const char *log_lvl)
208 {
209 	unsigned long addr;
210 
211 	printk("%sCall trace:\n", log_lvl);
212 
213 	while (!kstack_end(sp)) {
214 		addr = *sp++;
215 		if (kernel_text_address(addr)) {
216 #ifdef CONFIG_KALLSYMS
217 			printk("%s [<%08lx>] ", log_lvl, addr);
218 #else
219 			printk(" [<%08lx>] ", addr);
220 #endif
221 			print_symbol("%s\n", addr);
222 		}
223 	}
224 	printk("\n");
225 }
226 #endif
227 
show_stack_log_lvl(struct task_struct * tsk,unsigned long sp,struct pt_regs * regs,const char * log_lvl)228 void show_stack_log_lvl(struct task_struct *tsk, unsigned long sp,
229 			struct pt_regs *regs, const char *log_lvl)
230 {
231 	struct thread_info *tinfo;
232 
233 	if (sp == 0) {
234 		if (tsk)
235 			sp = tsk->thread.cpu_context.ksp;
236 		else
237 			sp = (unsigned long)&tinfo;
238 	}
239 	if (!tsk)
240 		tsk = current;
241 
242 	tinfo = task_thread_info(tsk);
243 
244 	if (valid_stack_ptr(tinfo, sp)) {
245 		dump_mem("Stack: ", log_lvl, sp,
246 			 THREAD_SIZE + (unsigned long)tinfo);
247 		show_trace_log_lvl(tsk, (unsigned long *)sp, regs, log_lvl);
248 	}
249 }
250 
show_stack(struct task_struct * tsk,unsigned long * stack)251 void show_stack(struct task_struct *tsk, unsigned long *stack)
252 {
253 	show_stack_log_lvl(tsk, (unsigned long)stack, NULL, "");
254 }
255 
dump_stack(void)256 void dump_stack(void)
257 {
258 	unsigned long stack;
259 
260 	show_trace_log_lvl(current, &stack, NULL, "");
261 }
262 EXPORT_SYMBOL(dump_stack);
263 
264 static const char *cpu_modes[] = {
265 	"Application", "Supervisor", "Interrupt level 0", "Interrupt level 1",
266 	"Interrupt level 2", "Interrupt level 3", "Exception", "NMI"
267 };
268 
show_regs_log_lvl(struct pt_regs * regs,const char * log_lvl)269 void show_regs_log_lvl(struct pt_regs *regs, const char *log_lvl)
270 {
271 	unsigned long sp = regs->sp;
272 	unsigned long lr = regs->lr;
273 	unsigned long mode = (regs->sr & MODE_MASK) >> MODE_SHIFT;
274 
275 	if (!user_mode(regs)) {
276 		sp = (unsigned long)regs + FRAME_SIZE_FULL;
277 
278 		printk("%s", log_lvl);
279 		print_symbol("PC is at %s\n", instruction_pointer(regs));
280 		printk("%s", log_lvl);
281 		print_symbol("LR is at %s\n", lr);
282 	}
283 
284 	printk("%spc : [<%08lx>]    lr : [<%08lx>]    %s\n"
285 	       "%ssp : %08lx  r12: %08lx  r11: %08lx\n",
286 	       log_lvl, instruction_pointer(regs), lr, print_tainted(),
287 	       log_lvl, sp, regs->r12, regs->r11);
288 	printk("%sr10: %08lx  r9 : %08lx  r8 : %08lx\n",
289 	       log_lvl, regs->r10, regs->r9, regs->r8);
290 	printk("%sr7 : %08lx  r6 : %08lx  r5 : %08lx  r4 : %08lx\n",
291 	       log_lvl, regs->r7, regs->r6, regs->r5, regs->r4);
292 	printk("%sr3 : %08lx  r2 : %08lx  r1 : %08lx  r0 : %08lx\n",
293 	       log_lvl, regs->r3, regs->r2, regs->r1, regs->r0);
294 	printk("%sFlags: %c%c%c%c%c\n", log_lvl,
295 	       regs->sr & SR_Q ? 'Q' : 'q',
296 	       regs->sr & SR_V ? 'V' : 'v',
297 	       regs->sr & SR_N ? 'N' : 'n',
298 	       regs->sr & SR_Z ? 'Z' : 'z',
299 	       regs->sr & SR_C ? 'C' : 'c');
300 	printk("%sMode bits: %c%c%c%c%c%c%c%c%c%c\n", log_lvl,
301 	       regs->sr & SR_H ? 'H' : 'h',
302 	       regs->sr & SR_J ? 'J' : 'j',
303 	       regs->sr & SR_DM ? 'M' : 'm',
304 	       regs->sr & SR_D ? 'D' : 'd',
305 	       regs->sr & SR_EM ? 'E' : 'e',
306 	       regs->sr & SR_I3M ? '3' : '.',
307 	       regs->sr & SR_I2M ? '2' : '.',
308 	       regs->sr & SR_I1M ? '1' : '.',
309 	       regs->sr & SR_I0M ? '0' : '.',
310 	       regs->sr & SR_GM ? 'G' : 'g');
311 	printk("%sCPU Mode: %s\n", log_lvl, cpu_modes[mode]);
312 	printk("%sProcess: %s [%d] (task: %p thread: %p)\n",
313 	       log_lvl, current->comm, current->pid, current,
314 	       task_thread_info(current));
315 }
316 
show_regs(struct pt_regs * regs)317 void show_regs(struct pt_regs *regs)
318 {
319 	unsigned long sp = regs->sp;
320 
321 	if (!user_mode(regs))
322 		sp = (unsigned long)regs + FRAME_SIZE_FULL;
323 
324 	show_regs_log_lvl(regs, "");
325 	show_trace_log_lvl(current, (unsigned long *)sp, regs, "");
326 }
327 EXPORT_SYMBOL(show_regs);
328 
329 /* Fill in the fpu structure for a core dump. This is easy -- we don't have any */
dump_fpu(struct pt_regs * regs,elf_fpregset_t * fpu)330 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
331 {
332 	/* Not valid */
333 	return 0;
334 }
335 
336 asmlinkage void ret_from_fork(void);
337 
copy_thread(unsigned long clone_flags,unsigned long usp,unsigned long unused,struct task_struct * p,struct pt_regs * regs)338 int copy_thread(unsigned long clone_flags, unsigned long usp,
339 		unsigned long unused,
340 		struct task_struct *p, struct pt_regs *regs)
341 {
342 	struct pt_regs *childregs;
343 
344 	childregs = ((struct pt_regs *)(THREAD_SIZE + (unsigned long)task_stack_page(p))) - 1;
345 	*childregs = *regs;
346 
347 	if (user_mode(regs))
348 		childregs->sp = usp;
349 	else
350 		childregs->sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
351 
352 	childregs->r12 = 0; /* Set return value for child */
353 
354 	p->thread.cpu_context.sr = MODE_SUPERVISOR | SR_GM;
355 	p->thread.cpu_context.ksp = (unsigned long)childregs;
356 	p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
357 
358 	clear_tsk_thread_flag(p, TIF_DEBUG);
359 	if ((clone_flags & CLONE_PTRACE) && test_thread_flag(TIF_DEBUG))
360 		ocd_enable(p);
361 
362 	return 0;
363 }
364 
365 /* r12-r8 are dummy parameters to force the compiler to use the stack */
sys_fork(struct pt_regs * regs)366 asmlinkage int sys_fork(struct pt_regs *regs)
367 {
368 	return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
369 }
370 
sys_clone(unsigned long clone_flags,unsigned long newsp,void __user * parent_tidptr,void __user * child_tidptr,struct pt_regs * regs)371 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
372 		void __user *parent_tidptr, void __user *child_tidptr,
373 		struct pt_regs *regs)
374 {
375 	if (!newsp)
376 		newsp = regs->sp;
377 	return do_fork(clone_flags, newsp, regs, 0, parent_tidptr,
378 			child_tidptr);
379 }
380 
sys_vfork(struct pt_regs * regs)381 asmlinkage int sys_vfork(struct pt_regs *regs)
382 {
383 	return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs,
384 		       0, NULL, NULL);
385 }
386 
sys_execve(const char __user * ufilename,const char __user * const __user * uargv,const char __user * const __user * uenvp,struct pt_regs * regs)387 asmlinkage int sys_execve(const char __user *ufilename,
388 			  const char __user *const __user *uargv,
389 			  const char __user *const __user *uenvp,
390 			  struct pt_regs *regs)
391 {
392 	int error;
393 	char *filename;
394 
395 	filename = getname(ufilename);
396 	error = PTR_ERR(filename);
397 	if (IS_ERR(filename))
398 		goto out;
399 
400 	error = do_execve(filename, uargv, uenvp, regs);
401 	putname(filename);
402 
403 out:
404 	return error;
405 }
406 
407 
408 /*
409  * This function is supposed to answer the question "who called
410  * schedule()?"
411  */
get_wchan(struct task_struct * p)412 unsigned long get_wchan(struct task_struct *p)
413 {
414 	unsigned long pc;
415 	unsigned long stack_page;
416 
417 	if (!p || p == current || p->state == TASK_RUNNING)
418 		return 0;
419 
420 	stack_page = (unsigned long)task_stack_page(p);
421 	BUG_ON(!stack_page);
422 
423 	/*
424 	 * The stored value of PC is either the address right after
425 	 * the call to __switch_to() or ret_from_fork.
426 	 */
427 	pc = thread_saved_pc(p);
428 	if (in_sched_functions(pc)) {
429 #ifdef CONFIG_FRAME_POINTER
430 		unsigned long fp = p->thread.cpu_context.r7;
431 		BUG_ON(fp < stack_page || fp > (THREAD_SIZE + stack_page));
432 		pc = *(unsigned long *)fp;
433 #else
434 		/*
435 		 * We depend on the frame size of schedule here, which
436 		 * is actually quite ugly. It might be possible to
437 		 * determine the frame size automatically at build
438 		 * time by doing this:
439 		 *   - compile sched.c
440 		 *   - disassemble the resulting sched.o
441 		 *   - look for 'sub sp,??' shortly after '<schedule>:'
442 		 */
443 		unsigned long sp = p->thread.cpu_context.ksp + 16;
444 		BUG_ON(sp < stack_page || sp > (THREAD_SIZE + stack_page));
445 		pc = *(unsigned long *)sp;
446 #endif
447 	}
448 
449 	return pc;
450 }
451