]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/avr32/kernel/process.c
sched/headers: Prepare for new header dependencies before moving code to <linux/sched...
[mirror_ubuntu-bionic-kernel.git] / arch / avr32 / kernel / process.c
CommitLineData
5f97f7f9
HS
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>
b17b0153 9#include <linux/sched/debug.h>
29930025 10#include <linux/sched/task.h>
5f97f7f9
HS
11#include <linux/module.h>
12#include <linux/kallsyms.h>
13#include <linux/fs.h>
b3bc2c55 14#include <linux/pm.h>
5f97f7f9 15#include <linux/ptrace.h>
5a0e3ad6 16#include <linux/slab.h>
5f97f7f9 17#include <linux/reboot.h>
d45ad062 18#include <linux/tick.h>
623b0355 19#include <linux/uaccess.h>
5f97f7f9
HS
20#include <linux/unistd.h>
21
22#include <asm/sysreg.h>
23#include <asm/ocd.h>
c80ce2d5 24#include <asm/syscalls.h>
5f97f7f9 25
3663b736 26#include <mach/pm.h>
7e59128f 27
b3bc2c55 28void (*pm_power_off)(void);
5f97f7f9
HS
29EXPORT_SYMBOL(pm_power_off);
30
31/*
32 * This file handles the architecture-dependent parts of process handling..
33 */
34
01426478 35void arch_cpu_idle(void)
5f97f7f9 36{
01426478 37 cpu_enter_idle();
5f97f7f9
HS
38}
39
40void machine_halt(void)
41{
c2eb5090
HS
42 /*
43 * Enter Stop mode. The 32 kHz oscillator will keep running so
44 * the RTC will keep the time properly and the system will
45 * boot quickly.
46 */
47 asm volatile("sleep 3\n\t"
48 "sub pc, -2");
5f97f7f9
HS
49}
50
51void machine_power_off(void)
52{
ed3fa7c9
PM
53 if (pm_power_off)
54 pm_power_off();
5f97f7f9
HS
55}
56
57void machine_restart(char *cmd)
58{
8dfe8f29
HS
59 ocd_write(DC, (1 << OCD_DC_DBE_BIT));
60 ocd_write(DC, (1 << OCD_DC_RES_BIT));
5f97f7f9
HS
61 while (1) ;
62}
63
5f97f7f9
HS
64/*
65 * Free current thread data structures etc
66 */
e6464694 67void exit_thread(struct task_struct *tsk)
5f97f7f9 68{
e6464694 69 ocd_disable(tsk);
5f97f7f9
HS
70}
71
72void flush_thread(void)
73{
74 /* nothing to do */
75}
76
77void release_thread(struct task_struct *dead_task)
78{
79 /* do nothing */
80}
81
623b0355
HS
82static void dump_mem(const char *str, const char *log_lvl,
83 unsigned long bottom, unsigned long top)
84{
85 unsigned long p;
86 int i;
87
88 printk("%s%s(0x%08lx to 0x%08lx)\n", log_lvl, str, bottom, top);
89
90 for (p = bottom & ~31; p < top; ) {
91 printk("%s%04lx: ", log_lvl, p & 0xffff);
92
93 for (i = 0; i < 8; i++, p += 4) {
94 unsigned int val;
95
96 if (p < bottom || p >= top)
97 printk(" ");
98 else {
99 if (__get_user(val, (unsigned int __user *)p)) {
100 printk("\n");
101 goto out;
102 }
103 printk("%08x ", val);
104 }
105 }
106 printk("\n");
107 }
108
109out:
110 return;
111}
112
113static inline int valid_stack_ptr(struct thread_info *tinfo, unsigned long p)
114{
115 return (p > (unsigned long)tinfo)
116 && (p < (unsigned long)tinfo + THREAD_SIZE - 3);
117}
118
119#ifdef CONFIG_FRAME_POINTER
120static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp,
121 struct pt_regs *regs, const char *log_lvl)
122{
123 unsigned long lr, fp;
124 struct thread_info *tinfo;
125
126 if (regs)
127 fp = regs->r7;
128 else if (tsk == current)
129 asm("mov %0, r7" : "=r"(fp));
130 else
131 fp = tsk->thread.cpu_context.r7;
132
133 /*
134 * Walk the stack as long as the frame pointer (a) is within
135 * the kernel stack of the task, and (b) it doesn't move
136 * downwards.
137 */
138 tinfo = task_thread_info(tsk);
139 printk("%sCall trace:\n", log_lvl);
140 while (valid_stack_ptr(tinfo, fp)) {
141 unsigned long new_fp;
142
143 lr = *(unsigned long *)fp;
144#ifdef CONFIG_KALLSYMS
145 printk("%s [<%08lx>] ", log_lvl, lr);
146#else
147 printk(" [<%08lx>] ", lr);
148#endif
149 print_symbol("%s\n", lr);
150
151 new_fp = *(unsigned long *)(fp + 4);
152 if (new_fp <= fp)
153 break;
154 fp = new_fp;
155 }
156 printk("\n");
157}
158#else
159static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp,
160 struct pt_regs *regs, const char *log_lvl)
161{
162 unsigned long addr;
163
164 printk("%sCall trace:\n", log_lvl);
165
166 while (!kstack_end(sp)) {
167 addr = *sp++;
168 if (kernel_text_address(addr)) {
169#ifdef CONFIG_KALLSYMS
170 printk("%s [<%08lx>] ", log_lvl, addr);
171#else
172 printk(" [<%08lx>] ", addr);
173#endif
174 print_symbol("%s\n", addr);
175 }
176 }
177 printk("\n");
178}
179#endif
180
181void show_stack_log_lvl(struct task_struct *tsk, unsigned long sp,
182 struct pt_regs *regs, const char *log_lvl)
183{
184 struct thread_info *tinfo;
185
186 if (sp == 0) {
187 if (tsk)
188 sp = tsk->thread.cpu_context.ksp;
189 else
190 sp = (unsigned long)&tinfo;
191 }
192 if (!tsk)
193 tsk = current;
194
195 tinfo = task_thread_info(tsk);
196
197 if (valid_stack_ptr(tinfo, sp)) {
198 dump_mem("Stack: ", log_lvl, sp,
199 THREAD_SIZE + (unsigned long)tinfo);
200 show_trace_log_lvl(tsk, (unsigned long *)sp, regs, log_lvl);
201 }
202}
203
204void show_stack(struct task_struct *tsk, unsigned long *stack)
205{
206 show_stack_log_lvl(tsk, (unsigned long)stack, NULL, "");
207}
208
5f97f7f9
HS
209static const char *cpu_modes[] = {
210 "Application", "Supervisor", "Interrupt level 0", "Interrupt level 1",
211 "Interrupt level 2", "Interrupt level 3", "Exception", "NMI"
212};
213
623b0355 214void show_regs_log_lvl(struct pt_regs *regs, const char *log_lvl)
5f97f7f9
HS
215{
216 unsigned long sp = regs->sp;
217 unsigned long lr = regs->lr;
218 unsigned long mode = (regs->sr & MODE_MASK) >> MODE_SHIFT;
219
a43cb95d
TH
220 show_regs_print_info(log_lvl);
221
623b0355 222 if (!user_mode(regs)) {
5f97f7f9
HS
223 sp = (unsigned long)regs + FRAME_SIZE_FULL;
224
623b0355
HS
225 printk("%s", log_lvl);
226 print_symbol("PC is at %s\n", instruction_pointer(regs));
227 printk("%s", log_lvl);
228 print_symbol("LR is at %s\n", lr);
229 }
230
231 printk("%spc : [<%08lx>] lr : [<%08lx>] %s\n"
232 "%ssp : %08lx r12: %08lx r11: %08lx\n",
233 log_lvl, instruction_pointer(regs), lr, print_tainted(),
234 log_lvl, sp, regs->r12, regs->r11);
235 printk("%sr10: %08lx r9 : %08lx r8 : %08lx\n",
236 log_lvl, regs->r10, regs->r9, regs->r8);
237 printk("%sr7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
238 log_lvl, regs->r7, regs->r6, regs->r5, regs->r4);
239 printk("%sr3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
240 log_lvl, regs->r3, regs->r2, regs->r1, regs->r0);
241 printk("%sFlags: %c%c%c%c%c\n", log_lvl,
5f97f7f9
HS
242 regs->sr & SR_Q ? 'Q' : 'q',
243 regs->sr & SR_V ? 'V' : 'v',
244 regs->sr & SR_N ? 'N' : 'n',
245 regs->sr & SR_Z ? 'Z' : 'z',
246 regs->sr & SR_C ? 'C' : 'c');
df679771 247 printk("%sMode bits: %c%c%c%c%c%c%c%c%c%c\n", log_lvl,
5f97f7f9 248 regs->sr & SR_H ? 'H' : 'h',
5f97f7f9 249 regs->sr & SR_J ? 'J' : 'j',
df679771
HS
250 regs->sr & SR_DM ? 'M' : 'm',
251 regs->sr & SR_D ? 'D' : 'd',
5f97f7f9
HS
252 regs->sr & SR_EM ? 'E' : 'e',
253 regs->sr & SR_I3M ? '3' : '.',
254 regs->sr & SR_I2M ? '2' : '.',
255 regs->sr & SR_I1M ? '1' : '.',
256 regs->sr & SR_I0M ? '0' : '.',
257 regs->sr & SR_GM ? 'G' : 'g');
623b0355 258 printk("%sCPU Mode: %s\n", log_lvl, cpu_modes[mode]);
623b0355
HS
259}
260
261void show_regs(struct pt_regs *regs)
262{
263 unsigned long sp = regs->sp;
264
265 if (!user_mode(regs))
266 sp = (unsigned long)regs + FRAME_SIZE_FULL;
5f97f7f9 267
623b0355
HS
268 show_regs_log_lvl(regs, "");
269 show_trace_log_lvl(current, (unsigned long *)sp, regs, "");
5f97f7f9
HS
270}
271EXPORT_SYMBOL(show_regs);
272
273/* Fill in the fpu structure for a core dump. This is easy -- we don't have any */
274int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
275{
276 /* Not valid */
277 return 0;
278}
279
280asmlinkage void ret_from_fork(void);
5adc807f
AV
281asmlinkage void ret_from_kernel_thread(void);
282asmlinkage void syscall_return(void);
5f97f7f9 283
6f2c55b8 284int copy_thread(unsigned long clone_flags, unsigned long usp,
5adc807f 285 unsigned long arg,
afa86fc4 286 struct task_struct *p)
5f97f7f9 287{
5adc807f
AV
288 struct pt_regs *childregs = task_pt_regs(p);
289
584271bc 290 if (unlikely(p->flags & PF_KTHREAD)) {
5adc807f
AV
291 memset(childregs, 0, sizeof(struct pt_regs));
292 p->thread.cpu_context.r0 = arg;
293 p->thread.cpu_context.r1 = usp; /* fn */
395e73a2 294 p->thread.cpu_context.r2 = (unsigned long)syscall_return;
5adc807f
AV
295 p->thread.cpu_context.pc = (unsigned long)ret_from_kernel_thread;
296 childregs->sr = MODE_SUPERVISOR;
297 } else {
584271bc
AV
298 *childregs = *current_pt_regs();
299 if (usp)
300 childregs->sp = usp;
5adc807f
AV
301 childregs->r12 = 0; /* Set return value for child */
302 p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
303 }
5f97f7f9
HS
304
305 p->thread.cpu_context.sr = MODE_SUPERVISOR | SR_GM;
306 p->thread.cpu_context.ksp = (unsigned long)childregs;
5f97f7f9 307
325d6f55 308 clear_tsk_thread_flag(p, TIF_DEBUG);
13b54a50
HS
309 if ((clone_flags & CLONE_PTRACE) && test_thread_flag(TIF_DEBUG))
310 ocd_enable(p);
311
5f97f7f9
HS
312 return 0;
313}
314
5f97f7f9
HS
315/*
316 * This function is supposed to answer the question "who called
317 * schedule()?"
318 */
319unsigned long get_wchan(struct task_struct *p)
320{
321 unsigned long pc;
322 unsigned long stack_page;
323
324 if (!p || p == current || p->state == TASK_RUNNING)
325 return 0;
326
c9f4f06d 327 stack_page = (unsigned long)task_stack_page(p);
5f97f7f9
HS
328 BUG_ON(!stack_page);
329
330 /*
331 * The stored value of PC is either the address right after
332 * the call to __switch_to() or ret_from_fork.
333 */
334 pc = thread_saved_pc(p);
335 if (in_sched_functions(pc)) {
336#ifdef CONFIG_FRAME_POINTER
337 unsigned long fp = p->thread.cpu_context.r7;
338 BUG_ON(fp < stack_page || fp > (THREAD_SIZE + stack_page));
339 pc = *(unsigned long *)fp;
340#else
341 /*
342 * We depend on the frame size of schedule here, which
343 * is actually quite ugly. It might be possible to
344 * determine the frame size automatically at build
345 * time by doing this:
0a0fca9d 346 * - compile sched/core.c
5f97f7f9
HS
347 * - disassemble the resulting sched.o
348 * - look for 'sub sp,??' shortly after '<schedule>:'
349 */
350 unsigned long sp = p->thread.cpu_context.ksp + 16;
351 BUG_ON(sp < stack_page || sp > (THREAD_SIZE + stack_page));
352 pc = *(unsigned long *)sp;
353#endif
354 }
355
356 return pc;
357}