blob: 2eb7fa5bf9d8d48386ea7583dd1731dd92eff7a8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
David Howells9dec17e2006-07-10 04:44:51 -070013#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/errno.h>
15#include <linux/sched.h>
16#include <linux/kernel.h>
17#include <linux/mm.h>
18#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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>
Christoph Lameter8defab32007-05-09 02:32:48 -070027#include <linux/pagemap.h>
Frederic Weisbecker41d8fe52012-08-22 17:27:34 +020028#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
David Howells84e8cd62006-07-10 04:44:55 -070030#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/setup.h>
33#include <asm/pgtable.h>
Christoph Lameter8defab32007-05-09 02:32:48 -070034#include <asm/tlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/gdb-stub.h>
36#include <asm/mb-regs.h>
37
38#include "local.h"
39
40asmlinkage void ret_from_fork(void);
41
42#include <asm/pgalloc.h>
43
David Howells9dec17e2006-07-10 04:44:51 -070044void (*pm_power_off)(void);
45EXPORT_SYMBOL(pm_power_off);
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static 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
61void (*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 */
69void cpu_idle(void)
70{
71 /* endless idle loop with no priority at all */
72 while (1) {
Frederic Weisbecker41d8fe52012-08-22 17:27:34 +020073 rcu_idle_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 while (!need_resched()) {
Christoph Lameter8defab32007-05-09 02:32:48 -070075 check_pgt_cache();
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (!frv_dma_inprogress && idle)
78 idle();
79 }
Frederic Weisbecker41d8fe52012-08-22 17:27:34 +020080 rcu_idle_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Thomas Gleixnerbd2f5532011-03-21 12:33:18 +010082 schedule_preempt_disabled();
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84}
85
86void machine_restart(char * __unused)
87{
88 unsigned long reset_addr;
89#ifdef CONFIG_GDBSTUB
90 gdbstub_exit(0);
91#endif
92
93 if (PSR_IMPLE(__get_PSR()) == PSR_IMPLE_FR551)
94 reset_addr = 0xfefff500;
95 else
96 reset_addr = 0xfeff0500;
97
98 /* Software reset. */
99 asm volatile(" dcef @(gr0,gr0),1 ! membar !"
100 " sti %1,@(%0,0) !"
101 " nop ! nop ! nop ! nop ! nop ! "
102 " nop ! nop ! nop ! nop ! nop ! "
103 " nop ! nop ! nop ! nop ! nop ! "
104 " nop ! nop ! nop ! nop ! nop ! "
105 : : "r" (reset_addr), "r" (1) );
106
107 for (;;)
108 ;
109}
110
111void machine_halt(void)
112{
113#ifdef CONFIG_GDBSTUB
114 gdbstub_exit(0);
115#endif
116
117 for (;;);
118}
119
120void machine_power_off(void)
121{
122#ifdef CONFIG_GDBSTUB
123 gdbstub_exit(0);
124#endif
125
126 for (;;);
127}
128
129void flush_thread(void)
130{
Mathias Krauseadc400f2011-07-26 16:08:15 -0700131 /* nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134inline unsigned long user_stack(const struct pt_regs *regs)
135{
136 while (regs->next_frame)
137 regs = regs->next_frame;
138 return user_mode(regs) ? regs->sp : 0;
139}
140
141asmlinkage int sys_fork(void)
142{
143#ifndef CONFIG_MMU
144 /* fork almost works, enough to trick you into looking elsewhere:-( */
145 return -EINVAL;
146#else
147 return do_fork(SIGCHLD, user_stack(__frame), __frame, 0, NULL, NULL);
148#endif
149}
150
151asmlinkage int sys_vfork(void)
152{
153 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, user_stack(__frame), __frame, 0,
154 NULL, NULL);
155}
156
157/*****************************************************************************/
158/*
159 * clone a process
160 * - tlsptr is retrieved by copy_thread()
161 */
162asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
163 int __user *parent_tidptr, int __user *child_tidptr,
164 int __user *tlsptr)
165{
166 if (!newsp)
167 newsp = user_stack(__frame);
168 return do_fork(clone_flags, newsp, __frame, 0, parent_tidptr, child_tidptr);
169} /* end sys_clone() */
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/*
172 * set up the kernel stack and exception frames for a new process
173 */
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700174int copy_thread(unsigned long clone_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 unsigned long usp, unsigned long topstk,
176 struct task_struct *p, struct pt_regs *regs)
177{
178 struct pt_regs *childregs0, *childregs, *regs0;
179
180 regs0 = __kernel_frame0_ptr;
181 childregs0 = (struct pt_regs *)
David Howells84e8cd62006-07-10 04:44:55 -0700182 (task_stack_page(p) + THREAD_SIZE - FRV_FRAME0_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 childregs = childregs0;
184
185 /* set up the userspace frame (the only place that the USP is stored) */
186 *childregs0 = *regs0;
187
188 childregs0->gr8 = 0;
189 childregs0->sp = usp;
190 childregs0->next_frame = NULL;
191
192 /* set up the return kernel frame if called from kernel_thread() */
193 if (regs != regs0) {
194 childregs--;
195 *childregs = *regs;
196 childregs->sp = (unsigned long) childregs0;
197 childregs->next_frame = childregs0;
Al Viro097cb332006-01-12 01:05:53 -0800198 childregs->gr15 = (unsigned long) task_thread_info(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 childregs->gr29 = (unsigned long) p;
200 }
201
202 p->set_child_tid = p->clear_child_tid = NULL;
203
204 p->thread.frame = childregs;
205 p->thread.curr = p;
206 p->thread.sp = (unsigned long) childregs;
207 p->thread.fp = 0;
208 p->thread.lr = 0;
209 p->thread.pc = (unsigned long) ret_from_fork;
210 p->thread.frame0 = childregs0;
211
212 /* the new TLS pointer is passed in as arg #5 to sys_clone() */
213 if (clone_flags & CLONE_SETTLS)
214 childregs->gr29 = childregs->gr12;
215
216 save_user_regs(p->thread.user);
217
218 return 0;
219} /* end copy_thread() */
220
221/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * sys_execve() executes a new program.
223 */
David Howellsd7627462010-08-17 23:52:56 +0100224asmlinkage int sys_execve(const char __user *name,
225 const char __user *const __user *argv,
226 const char __user *const __user *envp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 int error;
229 char * filename;
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 filename = getname(name);
232 error = PTR_ERR(filename);
233 if (IS_ERR(filename))
John Kacurb69975a2009-10-12 22:53:25 +0200234 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 error = do_execve(filename, argv, envp, __frame);
236 putname(filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return error;
238}
239
240unsigned long get_wchan(struct task_struct *p)
241{
242 struct pt_regs *regs0;
243 unsigned long fp, pc;
244 unsigned long stack_limit;
245 int count = 0;
246 if (!p || p == current || p->state == TASK_RUNNING)
247 return 0;
248
249 stack_limit = (unsigned long) (p + 1);
250 fp = p->thread.fp;
251 regs0 = p->thread.frame0;
252
253 do {
254 if (fp < stack_limit || fp >= (unsigned long) regs0 || fp & 3)
255 return 0;
256
257 pc = ((unsigned long *) fp)[2];
258
259 /* FIXME: This depends on the order of these functions. */
260 if (!in_sched_functions(pc))
261 return pc;
262
263 fp = *(unsigned long *) fp;
264 } while (count++ < 16);
265
266 return 0;
267}
268
269unsigned long thread_saved_pc(struct task_struct *tsk)
270{
271 /* Check whether the thread is blocked in resume() */
272 if (in_sched_functions(tsk->thread.pc))
273 return ((unsigned long *)tsk->thread.fp)[2];
274 else
275 return tsk->thread.pc;
276}
277
278int elf_check_arch(const struct elf32_hdr *hdr)
279{
280 unsigned long hsr0 = __get_HSR(0);
281 unsigned long psr = __get_PSR();
282
283 if (hdr->e_machine != EM_FRV)
284 return 0;
285
286 switch (hdr->e_flags & EF_FRV_GPR_MASK) {
287 case EF_FRV_GPR64:
288 if ((hsr0 & HSR0_GRN) == HSR0_GRN_32)
289 return 0;
290 case EF_FRV_GPR32:
291 case 0:
292 break;
293 default:
294 return 0;
295 }
296
297 switch (hdr->e_flags & EF_FRV_FPR_MASK) {
298 case EF_FRV_FPR64:
299 if ((hsr0 & HSR0_FRN) == HSR0_FRN_32)
300 return 0;
301 case EF_FRV_FPR32:
302 case EF_FRV_FPR_NONE:
303 case 0:
304 break;
305 default:
306 return 0;
307 }
308
309 if ((hdr->e_flags & EF_FRV_MULADD) == EF_FRV_MULADD)
310 if (PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
311 PSR_IMPLE(psr) != PSR_IMPLE_FR451)
312 return 0;
313
314 switch (hdr->e_flags & EF_FRV_CPU_MASK) {
315 case EF_FRV_CPU_GENERIC:
316 break;
317 case EF_FRV_CPU_FR300:
318 case EF_FRV_CPU_SIMPLE:
319 case EF_FRV_CPU_TOMCAT:
320 default:
321 return 0;
322 case EF_FRV_CPU_FR400:
323 if (PSR_IMPLE(psr) != PSR_IMPLE_FR401 &&
324 PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
325 PSR_IMPLE(psr) != PSR_IMPLE_FR451 &&
326 PSR_IMPLE(psr) != PSR_IMPLE_FR551)
327 return 0;
328 break;
329 case EF_FRV_CPU_FR450:
330 if (PSR_IMPLE(psr) != PSR_IMPLE_FR451)
331 return 0;
332 break;
333 case EF_FRV_CPU_FR500:
334 if (PSR_IMPLE(psr) != PSR_IMPLE_FR501)
335 return 0;
336 break;
337 case EF_FRV_CPU_FR550:
338 if (PSR_IMPLE(psr) != PSR_IMPLE_FR551)
339 return 0;
340 break;
341 }
342
343 return 1;
344}
David Howells6d8c4e32006-07-10 04:44:55 -0700345
346int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
347{
348 memcpy(fpregs,
349 &current->thread.user->f,
350 sizeof(current->thread.user->f));
351 return 1;
352}