blob: ad0dfccedb7928c808f8bfdedd7d0d1f65d46030 [file] [log] [blame]
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001/*
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>
Ingo Molnarb17b0152017-02-08 18:51:35 +01009#include <linux/sched/debug.h>
Ingo Molnar29930022017-02-08 18:51:36 +010010#include <linux/sched/task.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010011#include <linux/sched/task_stack.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070012#include <linux/module.h>
13#include <linux/kallsyms.h>
14#include <linux/fs.h>
Haavard Skinnemoenb3bc2c52008-07-28 13:05:19 +020015#include <linux/pm.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070016#include <linux/ptrace.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070018#include <linux/reboot.h>
Haavard Skinnemoend45ad062008-02-15 14:38:40 +010019#include <linux/tick.h>
Haavard Skinnemoen623b0352007-03-13 17:59:11 +010020#include <linux/uaccess.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070021#include <linux/unistd.h>
22
23#include <asm/sysreg.h>
24#include <asm/ocd.h>
Jaswinder Singhc80ce2d2008-07-25 13:48:38 +053025#include <asm/syscalls.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070026
Haavard Skinnemoen3663b732008-08-05 13:57:38 +020027#include <mach/pm.h>
Haavard Skinnemoen7e591282008-02-24 23:24:26 +010028
Haavard Skinnemoenb3bc2c52008-07-28 13:05:19 +020029void (*pm_power_off)(void);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070030EXPORT_SYMBOL(pm_power_off);
31
32/*
33 * This file handles the architecture-dependent parts of process handling..
34 */
35
Thomas Gleixner01426472013-03-21 22:49:40 +010036void arch_cpu_idle(void)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070037{
Thomas Gleixner01426472013-03-21 22:49:40 +010038 cpu_enter_idle();
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070039}
40
41void machine_halt(void)
42{
Haavard Skinnemoenc2eb5092006-12-04 12:01:36 +010043 /*
44 * Enter Stop mode. The 32 kHz oscillator will keep running so
45 * the RTC will keep the time properly and the system will
46 * boot quickly.
47 */
48 asm volatile("sleep 3\n\t"
49 "sub pc, -2");
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070050}
51
52void machine_power_off(void)
53{
Peter Maed3fa7c2008-04-19 00:24:25 -070054 if (pm_power_off)
55 pm_power_off();
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070056}
57
58void machine_restart(char *cmd)
59{
Haavard Skinnemoen8dfe8f22007-11-27 13:31:20 +010060 ocd_write(DC, (1 << OCD_DC_DBE_BIT));
61 ocd_write(DC, (1 << OCD_DC_RES_BIT));
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070062 while (1) ;
63}
64
65/*
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070066 * Free current thread data structures etc
67 */
Jiri Slabye6464692016-05-20 17:00:20 -070068void exit_thread(struct task_struct *tsk)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070069{
Jiri Slabye6464692016-05-20 17:00:20 -070070 ocd_disable(tsk);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070071}
72
73void flush_thread(void)
74{
75 /* nothing to do */
76}
77
78void release_thread(struct task_struct *dead_task)
79{
80 /* do nothing */
81}
82
Haavard Skinnemoen623b0352007-03-13 17:59:11 +010083static void dump_mem(const char *str, const char *log_lvl,
84 unsigned long bottom, unsigned long top)
85{
86 unsigned long p;
87 int i;
88
89 printk("%s%s(0x%08lx to 0x%08lx)\n", log_lvl, str, bottom, top);
90
91 for (p = bottom & ~31; p < top; ) {
92 printk("%s%04lx: ", log_lvl, p & 0xffff);
93
94 for (i = 0; i < 8; i++, p += 4) {
95 unsigned int val;
96
97 if (p < bottom || p >= top)
98 printk(" ");
99 else {
100 if (__get_user(val, (unsigned int __user *)p)) {
101 printk("\n");
102 goto out;
103 }
104 printk("%08x ", val);
105 }
106 }
107 printk("\n");
108 }
109
110out:
111 return;
112}
113
114static inline int valid_stack_ptr(struct thread_info *tinfo, unsigned long p)
115{
116 return (p > (unsigned long)tinfo)
117 && (p < (unsigned long)tinfo + THREAD_SIZE - 3);
118}
119
120#ifdef CONFIG_FRAME_POINTER
121static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp,
122 struct pt_regs *regs, const char *log_lvl)
123{
124 unsigned long lr, fp;
125 struct thread_info *tinfo;
126
127 if (regs)
128 fp = regs->r7;
129 else if (tsk == current)
130 asm("mov %0, r7" : "=r"(fp));
131 else
132 fp = tsk->thread.cpu_context.r7;
133
134 /*
135 * Walk the stack as long as the frame pointer (a) is within
136 * the kernel stack of the task, and (b) it doesn't move
137 * downwards.
138 */
139 tinfo = task_thread_info(tsk);
140 printk("%sCall trace:\n", log_lvl);
141 while (valid_stack_ptr(tinfo, fp)) {
142 unsigned long new_fp;
143
144 lr = *(unsigned long *)fp;
145#ifdef CONFIG_KALLSYMS
146 printk("%s [<%08lx>] ", log_lvl, lr);
147#else
148 printk(" [<%08lx>] ", lr);
149#endif
150 print_symbol("%s\n", lr);
151
152 new_fp = *(unsigned long *)(fp + 4);
153 if (new_fp <= fp)
154 break;
155 fp = new_fp;
156 }
157 printk("\n");
158}
159#else
160static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp,
161 struct pt_regs *regs, const char *log_lvl)
162{
163 unsigned long addr;
164
165 printk("%sCall trace:\n", log_lvl);
166
167 while (!kstack_end(sp)) {
168 addr = *sp++;
169 if (kernel_text_address(addr)) {
170#ifdef CONFIG_KALLSYMS
171 printk("%s [<%08lx>] ", log_lvl, addr);
172#else
173 printk(" [<%08lx>] ", addr);
174#endif
175 print_symbol("%s\n", addr);
176 }
177 }
178 printk("\n");
179}
180#endif
181
182void show_stack_log_lvl(struct task_struct *tsk, unsigned long sp,
183 struct pt_regs *regs, const char *log_lvl)
184{
185 struct thread_info *tinfo;
186
187 if (sp == 0) {
188 if (tsk)
189 sp = tsk->thread.cpu_context.ksp;
190 else
191 sp = (unsigned long)&tinfo;
192 }
193 if (!tsk)
194 tsk = current;
195
196 tinfo = task_thread_info(tsk);
197
198 if (valid_stack_ptr(tinfo, sp)) {
199 dump_mem("Stack: ", log_lvl, sp,
200 THREAD_SIZE + (unsigned long)tinfo);
201 show_trace_log_lvl(tsk, (unsigned long *)sp, regs, log_lvl);
202 }
203}
204
205void show_stack(struct task_struct *tsk, unsigned long *stack)
206{
207 show_stack_log_lvl(tsk, (unsigned long)stack, NULL, "");
208}
209
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700210static const char *cpu_modes[] = {
211 "Application", "Supervisor", "Interrupt level 0", "Interrupt level 1",
212 "Interrupt level 2", "Interrupt level 3", "Exception", "NMI"
213};
214
Haavard Skinnemoen623b0352007-03-13 17:59:11 +0100215void show_regs_log_lvl(struct pt_regs *regs, const char *log_lvl)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700216{
217 unsigned long sp = regs->sp;
218 unsigned long lr = regs->lr;
219 unsigned long mode = (regs->sr & MODE_MASK) >> MODE_SHIFT;
220
Tejun Heoa43cb952013-04-30 15:27:17 -0700221 show_regs_print_info(log_lvl);
222
Haavard Skinnemoen623b0352007-03-13 17:59:11 +0100223 if (!user_mode(regs)) {
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700224 sp = (unsigned long)regs + FRAME_SIZE_FULL;
225
Haavard Skinnemoen623b0352007-03-13 17:59:11 +0100226 printk("%s", log_lvl);
227 print_symbol("PC is at %s\n", instruction_pointer(regs));
228 printk("%s", log_lvl);
229 print_symbol("LR is at %s\n", lr);
230 }
231
232 printk("%spc : [<%08lx>] lr : [<%08lx>] %s\n"
233 "%ssp : %08lx r12: %08lx r11: %08lx\n",
234 log_lvl, instruction_pointer(regs), lr, print_tainted(),
235 log_lvl, sp, regs->r12, regs->r11);
236 printk("%sr10: %08lx r9 : %08lx r8 : %08lx\n",
237 log_lvl, regs->r10, regs->r9, regs->r8);
238 printk("%sr7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
239 log_lvl, regs->r7, regs->r6, regs->r5, regs->r4);
240 printk("%sr3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
241 log_lvl, regs->r3, regs->r2, regs->r1, regs->r0);
242 printk("%sFlags: %c%c%c%c%c\n", log_lvl,
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700243 regs->sr & SR_Q ? 'Q' : 'q',
244 regs->sr & SR_V ? 'V' : 'v',
245 regs->sr & SR_N ? 'N' : 'n',
246 regs->sr & SR_Z ? 'Z' : 'z',
247 regs->sr & SR_C ? 'C' : 'c');
Haavard Skinnemoendf679772007-11-27 18:59:32 +0100248 printk("%sMode bits: %c%c%c%c%c%c%c%c%c%c\n", log_lvl,
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700249 regs->sr & SR_H ? 'H' : 'h',
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700250 regs->sr & SR_J ? 'J' : 'j',
Haavard Skinnemoendf679772007-11-27 18:59:32 +0100251 regs->sr & SR_DM ? 'M' : 'm',
252 regs->sr & SR_D ? 'D' : 'd',
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700253 regs->sr & SR_EM ? 'E' : 'e',
254 regs->sr & SR_I3M ? '3' : '.',
255 regs->sr & SR_I2M ? '2' : '.',
256 regs->sr & SR_I1M ? '1' : '.',
257 regs->sr & SR_I0M ? '0' : '.',
258 regs->sr & SR_GM ? 'G' : 'g');
Haavard Skinnemoen623b0352007-03-13 17:59:11 +0100259 printk("%sCPU Mode: %s\n", log_lvl, cpu_modes[mode]);
Haavard Skinnemoen623b0352007-03-13 17:59:11 +0100260}
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700261
Haavard Skinnemoen623b0352007-03-13 17:59:11 +0100262void show_regs(struct pt_regs *regs)
263{
264 unsigned long sp = regs->sp;
265
266 if (!user_mode(regs))
267 sp = (unsigned long)regs + FRAME_SIZE_FULL;
268
269 show_regs_log_lvl(regs, "");
270 show_trace_log_lvl(current, (unsigned long *)sp, regs, "");
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700271}
272EXPORT_SYMBOL(show_regs);
273
274/* Fill in the fpu structure for a core dump. This is easy -- we don't have any */
275int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
276{
277 /* Not valid */
278 return 0;
279}
280
281asmlinkage void ret_from_fork(void);
Al Viro5adc8072012-10-15 02:23:22 -0400282asmlinkage void ret_from_kernel_thread(void);
283asmlinkage void syscall_return(void);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700284
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700285int copy_thread(unsigned long clone_flags, unsigned long usp,
Al Viro5adc8072012-10-15 02:23:22 -0400286 unsigned long arg,
Al Viroafa86fc2012-10-22 22:51:14 -0400287 struct task_struct *p)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700288{
Al Viro5adc8072012-10-15 02:23:22 -0400289 struct pt_regs *childregs = task_pt_regs(p);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700290
Al Viro584271b2012-10-21 15:57:32 -0400291 if (unlikely(p->flags & PF_KTHREAD)) {
Al Viro5adc8072012-10-15 02:23:22 -0400292 memset(childregs, 0, sizeof(struct pt_regs));
293 p->thread.cpu_context.r0 = arg;
294 p->thread.cpu_context.r1 = usp; /* fn */
Gabor Juhos395e73a2013-09-25 21:50:01 +0200295 p->thread.cpu_context.r2 = (unsigned long)syscall_return;
Al Viro5adc8072012-10-15 02:23:22 -0400296 p->thread.cpu_context.pc = (unsigned long)ret_from_kernel_thread;
297 childregs->sr = MODE_SUPERVISOR;
298 } else {
Al Viro584271b2012-10-21 15:57:32 -0400299 *childregs = *current_pt_regs();
300 if (usp)
301 childregs->sp = usp;
Al Viro5adc8072012-10-15 02:23:22 -0400302 childregs->r12 = 0; /* Set return value for child */
303 p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
304 }
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700305
306 p->thread.cpu_context.sr = MODE_SUPERVISOR | SR_GM;
307 p->thread.cpu_context.ksp = (unsigned long)childregs;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700308
Haavard Skinnemoen325d6f52008-02-27 14:04:29 +0100309 clear_tsk_thread_flag(p, TIF_DEBUG);
Haavard Skinnemoen13b54a52007-11-27 13:50:45 +0100310 if ((clone_flags & CLONE_PTRACE) && test_thread_flag(TIF_DEBUG))
311 ocd_enable(p);
312
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700313 return 0;
314}
315
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700316/*
317 * This function is supposed to answer the question "who called
318 * schedule()?"
319 */
320unsigned long get_wchan(struct task_struct *p)
321{
322 unsigned long pc;
323 unsigned long stack_page;
324
325 if (!p || p == current || p->state == TASK_RUNNING)
326 return 0;
327
Roman Zippelc9f4f062007-05-09 02:35:16 -0700328 stack_page = (unsigned long)task_stack_page(p);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700329 BUG_ON(!stack_page);
330
331 /*
332 * The stored value of PC is either the address right after
333 * the call to __switch_to() or ret_from_fork.
334 */
335 pc = thread_saved_pc(p);
336 if (in_sched_functions(pc)) {
337#ifdef CONFIG_FRAME_POINTER
338 unsigned long fp = p->thread.cpu_context.r7;
339 BUG_ON(fp < stack_page || fp > (THREAD_SIZE + stack_page));
340 pc = *(unsigned long *)fp;
341#else
342 /*
343 * We depend on the frame size of schedule here, which
344 * is actually quite ugly. It might be possible to
345 * determine the frame size automatically at build
346 * time by doing this:
Viresh Kumar0a0fca92013-06-04 13:10:24 +0530347 * - compile sched/core.c
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700348 * - disassemble the resulting sched.o
349 * - look for 'sub sp,??' shortly after '<schedule>:'
350 */
351 unsigned long sp = p->thread.cpu_context.ksp + 16;
352 BUG_ON(sp < stack_page || sp > (THREAD_SIZE + stack_page));
353 pc = *(unsigned long *)sp;
354#endif
355 }
356
357 return pc;
358}