arm: switch to saner kernel_execve() semantics
[firefly-linux-kernel-4.4.55.git] / arch / frv / kernel / process.c
1 /* process.c: FRV specific parts of process handling
2  *
3  * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  * - Derived from arch/m68k/kernel/process.c
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version
10  * 2 of the License, or (at your option) any later version.
11  */
12
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/smp.h>
19 #include <linux/stddef.h>
20 #include <linux/unistd.h>
21 #include <linux/ptrace.h>
22 #include <linux/slab.h>
23 #include <linux/user.h>
24 #include <linux/elf.h>
25 #include <linux/reboot.h>
26 #include <linux/interrupt.h>
27 #include <linux/pagemap.h>
28
29 #include <asm/asm-offsets.h>
30 #include <asm/uaccess.h>
31 #include <asm/setup.h>
32 #include <asm/pgtable.h>
33 #include <asm/tlb.h>
34 #include <asm/gdb-stub.h>
35 #include <asm/mb-regs.h>
36
37 #include "local.h"
38
39 asmlinkage void ret_from_fork(void);
40 asmlinkage void ret_from_kernel_thread(void);
41
42 #include <asm/pgalloc.h>
43
44 void (*pm_power_off)(void);
45 EXPORT_SYMBOL(pm_power_off);
46
47 static void core_sleep_idle(void)
48 {
49 #ifdef LED_DEBUG_SLEEP
50         /* Show that we're sleeping... */
51         __set_LEDS(0x55aa);
52 #endif
53         frv_cpu_core_sleep();
54 #ifdef LED_DEBUG_SLEEP
55         /* ... and that we woke up */
56         __set_LEDS(0);
57 #endif
58         mb();
59 }
60
61 void (*idle)(void) = core_sleep_idle;
62
63 /*
64  * The idle thread. There's no useful work to be
65  * done, so just try to conserve power and have a
66  * low exit latency (ie sit in a loop waiting for
67  * somebody to say that they'd like to reschedule)
68  */
69 void cpu_idle(void)
70 {
71         /* endless idle loop with no priority at all */
72         while (1) {
73                 while (!need_resched()) {
74                         check_pgt_cache();
75
76                         if (!frv_dma_inprogress && idle)
77                                 idle();
78                 }
79
80                 schedule_preempt_disabled();
81         }
82 }
83
84 void machine_restart(char * __unused)
85 {
86         unsigned long reset_addr;
87 #ifdef CONFIG_GDBSTUB
88         gdbstub_exit(0);
89 #endif
90
91         if (PSR_IMPLE(__get_PSR()) == PSR_IMPLE_FR551)
92                 reset_addr = 0xfefff500;
93         else
94                 reset_addr = 0xfeff0500;
95
96         /* Software reset. */
97         asm volatile("      dcef @(gr0,gr0),1 ! membar !"
98                      "      sti     %1,@(%0,0) !"
99                      "      nop ! nop ! nop ! nop ! nop ! "
100                      "      nop ! nop ! nop ! nop ! nop ! "
101                      "      nop ! nop ! nop ! nop ! nop ! "
102                      "      nop ! nop ! nop ! nop ! nop ! "
103                      : : "r" (reset_addr), "r" (1) );
104
105         for (;;)
106                 ;
107 }
108
109 void machine_halt(void)
110 {
111 #ifdef CONFIG_GDBSTUB
112         gdbstub_exit(0);
113 #endif
114
115         for (;;);
116 }
117
118 void machine_power_off(void)
119 {
120 #ifdef CONFIG_GDBSTUB
121         gdbstub_exit(0);
122 #endif
123
124         for (;;);
125 }
126
127 void flush_thread(void)
128 {
129         /* nothing */
130 }
131
132 inline unsigned long user_stack(const struct pt_regs *regs)
133 {
134         while (regs->next_frame)
135                 regs = regs->next_frame;
136         return user_mode(regs) ? regs->sp : 0;
137 }
138
139 asmlinkage int sys_fork(void)
140 {
141 #ifndef CONFIG_MMU
142         /* fork almost works, enough to trick you into looking elsewhere:-( */
143         return -EINVAL;
144 #else
145         return do_fork(SIGCHLD, user_stack(__frame), __frame, 0, NULL, NULL);
146 #endif
147 }
148
149 asmlinkage int sys_vfork(void)
150 {
151         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, user_stack(__frame), __frame, 0,
152                        NULL, NULL);
153 }
154
155 /*****************************************************************************/
156 /*
157  * clone a process
158  * - tlsptr is retrieved by copy_thread()
159  */
160 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
161                          int __user *parent_tidptr, int __user *child_tidptr,
162                          int __user *tlsptr)
163 {
164         if (!newsp)
165                 newsp = user_stack(__frame);
166         return do_fork(clone_flags, newsp, __frame, 0, parent_tidptr, child_tidptr);
167 } /* end sys_clone() */
168
169 /*
170  * set up the kernel stack and exception frames for a new process
171  */
172 int copy_thread(unsigned long clone_flags,
173                 unsigned long usp, unsigned long arg,
174                 struct task_struct *p, struct pt_regs *regs)
175 {
176         struct pt_regs *childregs;
177
178         childregs = (struct pt_regs *)
179                 (task_stack_page(p) + THREAD_SIZE - FRV_FRAME0_SIZE);
180
181         p->set_child_tid = p->clear_child_tid = NULL;
182
183         p->thread.frame  = childregs;
184         p->thread.curr   = p;
185         p->thread.sp     = (unsigned long) childregs;
186         p->thread.fp     = 0;
187         p->thread.lr     = 0;
188         p->thread.frame0 = childregs;
189
190         if (unlikely(!regs)) {
191                 memset(childregs, 0, sizeof(struct pt_regs));
192                 childregs->gr9 = usp; /* function */
193                 childregs->gr8 = arg;
194                 chilregs->psr = PSR_S;
195                 p->thread.pc = (unsigned long) ret_from_kernel_thread;
196                 save_user_regs(p->thread.user);
197                 return 0;
198         }
199
200         /* set up the userspace frame (the only place that the USP is stored) */
201         *childregs = *regs;
202
203         childregs->sp           = usp;
204         childregs->next_frame   = NULL;
205
206         p->thread.pc = (unsigned long) ret_from_fork;
207
208         /* the new TLS pointer is passed in as arg #5 to sys_clone() */
209         if (clone_flags & CLONE_SETTLS)
210                 childregs->gr29 = childregs->gr12;
211
212         save_user_regs(p->thread.user);
213
214         return 0;
215 } /* end copy_thread() */
216
217 unsigned long get_wchan(struct task_struct *p)
218 {
219         struct pt_regs *regs0;
220         unsigned long fp, pc;
221         unsigned long stack_limit;
222         int count = 0;
223         if (!p || p == current || p->state == TASK_RUNNING)
224                 return 0;
225
226         stack_limit = (unsigned long) (p + 1);
227         fp = p->thread.fp;
228         regs0 = p->thread.frame0;
229
230         do {
231                 if (fp < stack_limit || fp >= (unsigned long) regs0 || fp & 3)
232                         return 0;
233
234                 pc = ((unsigned long *) fp)[2];
235
236                 /* FIXME: This depends on the order of these functions. */
237                 if (!in_sched_functions(pc))
238                         return pc;
239
240                 fp = *(unsigned long *) fp;
241         } while (count++ < 16);
242
243         return 0;
244 }
245
246 unsigned long thread_saved_pc(struct task_struct *tsk)
247 {
248         /* Check whether the thread is blocked in resume() */
249         if (in_sched_functions(tsk->thread.pc))
250                 return ((unsigned long *)tsk->thread.fp)[2];
251         else
252                 return tsk->thread.pc;
253 }
254
255 int elf_check_arch(const struct elf32_hdr *hdr)
256 {
257         unsigned long hsr0 = __get_HSR(0);
258         unsigned long psr = __get_PSR();
259
260         if (hdr->e_machine != EM_FRV)
261                 return 0;
262
263         switch (hdr->e_flags & EF_FRV_GPR_MASK) {
264         case EF_FRV_GPR64:
265                 if ((hsr0 & HSR0_GRN) == HSR0_GRN_32)
266                         return 0;
267         case EF_FRV_GPR32:
268         case 0:
269                 break;
270         default:
271                 return 0;
272         }
273
274         switch (hdr->e_flags & EF_FRV_FPR_MASK) {
275         case EF_FRV_FPR64:
276                 if ((hsr0 & HSR0_FRN) == HSR0_FRN_32)
277                         return 0;
278         case EF_FRV_FPR32:
279         case EF_FRV_FPR_NONE:
280         case 0:
281                 break;
282         default:
283                 return 0;
284         }
285
286         if ((hdr->e_flags & EF_FRV_MULADD) == EF_FRV_MULADD)
287                 if (PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
288                     PSR_IMPLE(psr) != PSR_IMPLE_FR451)
289                         return 0;
290
291         switch (hdr->e_flags & EF_FRV_CPU_MASK) {
292         case EF_FRV_CPU_GENERIC:
293                 break;
294         case EF_FRV_CPU_FR300:
295         case EF_FRV_CPU_SIMPLE:
296         case EF_FRV_CPU_TOMCAT:
297         default:
298                 return 0;
299         case EF_FRV_CPU_FR400:
300                 if (PSR_IMPLE(psr) != PSR_IMPLE_FR401 &&
301                     PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
302                     PSR_IMPLE(psr) != PSR_IMPLE_FR451 &&
303                     PSR_IMPLE(psr) != PSR_IMPLE_FR551)
304                         return 0;
305                 break;
306         case EF_FRV_CPU_FR450:
307                 if (PSR_IMPLE(psr) != PSR_IMPLE_FR451)
308                         return 0;
309                 break;
310         case EF_FRV_CPU_FR500:
311                 if (PSR_IMPLE(psr) != PSR_IMPLE_FR501)
312                         return 0;
313                 break;
314         case EF_FRV_CPU_FR550:
315                 if (PSR_IMPLE(psr) != PSR_IMPLE_FR551)
316                         return 0;
317                 break;
318         }
319
320         return 1;
321 }
322
323 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
324 {
325         memcpy(fpregs,
326                &current->thread.user->f,
327                sizeof(current->thread.user->f));
328         return 1;
329 }