98c4bfe972e03604c7b8500c01a0de4e9e6dc301
[linux-2.6-microblaze.git] / arch / nios2 / kernel / process.c
1 /*
2  * Architecture-dependent parts of process handling.
3  *
4  * Copyright (C) 2013 Altera Corporation
5  * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
6  * Copyright (C) 2009 Wind River Systems Inc
7  *   Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
8  * Copyright (C) 2004 Microtronix Datacom Ltd
9  *
10  * This file is subject to the terms and conditions of the GNU General Public
11  * License.  See the file "COPYING" in the main directory of this archive
12  * for more details.
13  */
14
15 #include <linux/export.h>
16 #include <linux/sched.h>
17 #include <linux/sched/debug.h>
18 #include <linux/sched/task.h>
19 #include <linux/sched/task_stack.h>
20 #include <linux/mm_types.h>
21 #include <linux/tick.h>
22 #include <linux/uaccess.h>
23
24 #include <asm/unistd.h>
25 #include <asm/traps.h>
26 #include <asm/cpuinfo.h>
27
28 asmlinkage void ret_from_fork(void);
29 asmlinkage void ret_from_kernel_thread(void);
30
31 void (*pm_power_off)(void) = NULL;
32 EXPORT_SYMBOL(pm_power_off);
33
34 void arch_cpu_idle(void)
35 {
36         raw_local_irq_enable();
37 }
38
39 /*
40  * The development boards have no way to pull a board reset. Just jump to the
41  * cpu reset address and let the boot loader or the code in head.S take care of
42  * resetting peripherals.
43  */
44 void machine_restart(char *__unused)
45 {
46         pr_notice("Machine restart (%08x)...\n", cpuinfo.reset_addr);
47         local_irq_disable();
48         __asm__ __volatile__ (
49         "jmp    %0\n\t"
50         :
51         : "r" (cpuinfo.reset_addr)
52         : "r4");
53 }
54
55 void machine_halt(void)
56 {
57         pr_notice("Machine halt...\n");
58         local_irq_disable();
59         for (;;)
60                 ;
61 }
62
63 /*
64  * There is no way to power off the development boards. So just spin for now. If
65  * we ever have a way of resetting a board using a GPIO we should add that here.
66  */
67 void machine_power_off(void)
68 {
69         pr_notice("Machine power off...\n");
70         local_irq_disable();
71         for (;;)
72                 ;
73 }
74
75 void show_regs(struct pt_regs *regs)
76 {
77         pr_notice("\n");
78         show_regs_print_info(KERN_DEFAULT);
79
80         pr_notice("r1: %08lx r2: %08lx r3: %08lx r4: %08lx\n",
81                 regs->r1,  regs->r2,  regs->r3,  regs->r4);
82
83         pr_notice("r5: %08lx r6: %08lx r7: %08lx r8: %08lx\n",
84                 regs->r5,  regs->r6,  regs->r7,  regs->r8);
85
86         pr_notice("r9: %08lx r10: %08lx r11: %08lx r12: %08lx\n",
87                 regs->r9,  regs->r10, regs->r11, regs->r12);
88
89         pr_notice("r13: %08lx r14: %08lx r15: %08lx\n",
90                 regs->r13, regs->r14, regs->r15);
91
92         pr_notice("ra: %08lx fp:  %08lx sp: %08lx gp: %08lx\n",
93                 regs->ra,  regs->fp,  regs->sp,  regs->gp);
94
95         pr_notice("ea: %08lx estatus: %08lx\n",
96                 regs->ea,  regs->estatus);
97 }
98
99 void flush_thread(void)
100 {
101 }
102
103 int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
104 {
105         unsigned long clone_flags = args->flags;
106         unsigned long usp = args->stack;
107         unsigned long arg = args->stack_size;
108         unsigned long tls = args->tls;
109         struct pt_regs *childregs = task_pt_regs(p);
110         struct pt_regs *regs;
111         struct switch_stack *stack;
112         struct switch_stack *childstack =
113                 ((struct switch_stack *)childregs) - 1;
114
115         if (unlikely(p->flags & (PF_KTHREAD | PF_IO_WORKER))) {
116                 memset(childstack, 0,
117                         sizeof(struct switch_stack) + sizeof(struct pt_regs));
118
119                 childstack->r16 = usp;          /* fn */
120                 childstack->r17 = arg;
121                 childstack->ra = (unsigned long) ret_from_kernel_thread;
122                 childregs->estatus = STATUS_PIE;
123                 childregs->sp = (unsigned long) childstack;
124
125                 p->thread.ksp = (unsigned long) childstack;
126                 p->thread.kregs = childregs;
127                 return 0;
128         }
129
130         regs = current_pt_regs();
131         *childregs = *regs;
132         childregs->r2 = 0;      /* Set the return value for the child. */
133         childregs->r7 = 0;
134
135         stack = ((struct switch_stack *) regs) - 1;
136         *childstack = *stack;
137         childstack->ra = (unsigned long)ret_from_fork;
138         p->thread.kregs = childregs;
139         p->thread.ksp = (unsigned long) childstack;
140
141         if (usp)
142                 childregs->sp = usp;
143
144         /* Initialize tls register. */
145         if (clone_flags & CLONE_SETTLS)
146                 childstack->r23 = tls;
147
148         return 0;
149 }
150
151 /*
152  *      Generic dumping code. Used for panic and debug.
153  */
154 void dump(struct pt_regs *fp)
155 {
156         unsigned long   *sp;
157         unsigned char   *tp;
158         int             i;
159
160         pr_emerg("\nCURRENT PROCESS:\n\n");
161         pr_emerg("COMM=%s PID=%d\n", current->comm, current->pid);
162
163         if (current->mm) {
164                 pr_emerg("TEXT=%08x-%08x DATA=%08x-%08x BSS=%08x-%08x\n",
165                         (int) current->mm->start_code,
166                         (int) current->mm->end_code,
167                         (int) current->mm->start_data,
168                         (int) current->mm->end_data,
169                         (int) current->mm->end_data,
170                         (int) current->mm->brk);
171                 pr_emerg("USER-STACK=%08x  KERNEL-STACK=%08x\n\n",
172                         (int) current->mm->start_stack,
173                         (int)(((unsigned long) current) + THREAD_SIZE));
174         }
175
176         pr_emerg("PC: %08lx\n", fp->ea);
177         pr_emerg("SR: %08lx    SP: %08lx\n",
178                 (long) fp->estatus, (long) fp);
179
180         pr_emerg("r1: %08lx    r2: %08lx    r3: %08lx\n",
181                 fp->r1, fp->r2, fp->r3);
182
183         pr_emerg("r4: %08lx    r5: %08lx    r6: %08lx    r7: %08lx\n",
184                 fp->r4, fp->r5, fp->r6, fp->r7);
185         pr_emerg("r8: %08lx    r9: %08lx    r10: %08lx    r11: %08lx\n",
186                 fp->r8, fp->r9, fp->r10, fp->r11);
187         pr_emerg("r12: %08lx  r13: %08lx    r14: %08lx    r15: %08lx\n",
188                 fp->r12, fp->r13, fp->r14, fp->r15);
189         pr_emerg("or2: %08lx   ra: %08lx     fp: %08lx    sp: %08lx\n",
190                 fp->orig_r2, fp->ra, fp->fp, fp->sp);
191         pr_emerg("\nUSP: %08x   TRAPFRAME: %08x\n",
192                 (unsigned int) fp->sp, (unsigned int) fp);
193
194         pr_emerg("\nCODE:");
195         tp = ((unsigned char *) fp->ea) - 0x20;
196         for (sp = (unsigned long *) tp, i = 0; (i < 0x40);  i += 4) {
197                 if ((i % 0x10) == 0)
198                         pr_emerg("\n%08x: ", (int) (tp + i));
199                 pr_emerg("%08x ", (int) *sp++);
200         }
201         pr_emerg("\n");
202
203         pr_emerg("\nKERNEL STACK:");
204         tp = ((unsigned char *) fp) - 0x40;
205         for (sp = (unsigned long *) tp, i = 0; (i < 0xc0); i += 4) {
206                 if ((i % 0x10) == 0)
207                         pr_emerg("\n%08x: ", (int) (tp + i));
208                 pr_emerg("%08x ", (int) *sp++);
209         }
210         pr_emerg("\n");
211         pr_emerg("\n");
212
213         pr_emerg("\nUSER STACK:");
214         tp = (unsigned char *) (fp->sp - 0x10);
215         for (sp = (unsigned long *) tp, i = 0; (i < 0x80); i += 4) {
216                 if ((i % 0x10) == 0)
217                         pr_emerg("\n%08x: ", (int) (tp + i));
218                 pr_emerg("%08x ", (int) *sp++);
219         }
220         pr_emerg("\n\n");
221 }
222
223 unsigned long __get_wchan(struct task_struct *p)
224 {
225         unsigned long fp, pc;
226         unsigned long stack_page;
227         int count = 0;
228
229         stack_page = (unsigned long)p;
230         fp = ((struct switch_stack *)p->thread.ksp)->fp;        /* ;dgt2 */
231         do {
232                 if (fp < stack_page+sizeof(struct task_struct) ||
233                         fp >= 8184+stack_page)  /* ;dgt2;tmp */
234                         return 0;
235                 pc = ((unsigned long *)fp)[1];
236                 if (!in_sched_functions(pc))
237                         return pc;
238                 fp = *(unsigned long *) fp;
239         } while (count++ < 16);         /* ;dgt2;tmp */
240         return 0;
241 }
242
243 /*
244  * Do necessary setup to start up a newly executed thread.
245  * Will startup in user mode (status_extension = 0).
246  */
247 void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp)
248 {
249         memset((void *) regs, 0, sizeof(struct pt_regs));
250         regs->estatus = ESTATUS_EPIE | ESTATUS_EU;
251         regs->ea = pc;
252         regs->sp = sp;
253 }
254
255 asmlinkage int nios2_clone(unsigned long clone_flags, unsigned long newsp,
256                            int __user *parent_tidptr, int __user *child_tidptr,
257                            unsigned long tls)
258 {
259         struct kernel_clone_args args = {
260                 .flags          = (lower_32_bits(clone_flags) & ~CSIGNAL),
261                 .pidfd          = parent_tidptr,
262                 .child_tid      = child_tidptr,
263                 .parent_tid     = parent_tidptr,
264                 .exit_signal    = (lower_32_bits(clone_flags) & CSIGNAL),
265                 .stack          = newsp,
266                 .tls            = tls,
267         };
268
269         return kernel_clone(&args);
270 }