blob: c4dc7a4d229adc76d3ed4a1f8e8376e681c1bf27 [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 Weisbeckerc2723552012-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 Weisbeckerc2723552012-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 Weisbeckerc2723552012-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
171/*****************************************************************************/
172/*
173 * This gets called before we allocate a new thread and copy
174 * the current task into it.
175 */
176void prepare_to_copy(struct task_struct *tsk)
177{
178 //unlazy_fpu(tsk);
179} /* end prepare_to_copy() */
180
181/*****************************************************************************/
182/*
183 * set up the kernel stack and exception frames for a new process
184 */
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700185int copy_thread(unsigned long clone_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 unsigned long usp, unsigned long topstk,
187 struct task_struct *p, struct pt_regs *regs)
188{
189 struct pt_regs *childregs0, *childregs, *regs0;
190
191 regs0 = __kernel_frame0_ptr;
192 childregs0 = (struct pt_regs *)
David Howells84e8cd62006-07-10 04:44:55 -0700193 (task_stack_page(p) + THREAD_SIZE - FRV_FRAME0_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 childregs = childregs0;
195
196 /* set up the userspace frame (the only place that the USP is stored) */
197 *childregs0 = *regs0;
198
199 childregs0->gr8 = 0;
200 childregs0->sp = usp;
201 childregs0->next_frame = NULL;
202
203 /* set up the return kernel frame if called from kernel_thread() */
204 if (regs != regs0) {
205 childregs--;
206 *childregs = *regs;
207 childregs->sp = (unsigned long) childregs0;
208 childregs->next_frame = childregs0;
Al Viro097cb332006-01-12 01:05:53 -0800209 childregs->gr15 = (unsigned long) task_thread_info(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 childregs->gr29 = (unsigned long) p;
211 }
212
213 p->set_child_tid = p->clear_child_tid = NULL;
214
215 p->thread.frame = childregs;
216 p->thread.curr = p;
217 p->thread.sp = (unsigned long) childregs;
218 p->thread.fp = 0;
219 p->thread.lr = 0;
220 p->thread.pc = (unsigned long) ret_from_fork;
221 p->thread.frame0 = childregs0;
222
223 /* the new TLS pointer is passed in as arg #5 to sys_clone() */
224 if (clone_flags & CLONE_SETTLS)
225 childregs->gr29 = childregs->gr12;
226
227 save_user_regs(p->thread.user);
228
229 return 0;
230} /* end copy_thread() */
231
232/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 * sys_execve() executes a new program.
234 */
David Howellsd7627462010-08-17 23:52:56 +0100235asmlinkage int sys_execve(const char __user *name,
236 const char __user *const __user *argv,
237 const char __user *const __user *envp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 int error;
240 char * filename;
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 filename = getname(name);
243 error = PTR_ERR(filename);
244 if (IS_ERR(filename))
John Kacurb69975a2009-10-12 22:53:25 +0200245 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 error = do_execve(filename, argv, envp, __frame);
247 putname(filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return error;
249}
250
251unsigned long get_wchan(struct task_struct *p)
252{
253 struct pt_regs *regs0;
254 unsigned long fp, pc;
255 unsigned long stack_limit;
256 int count = 0;
257 if (!p || p == current || p->state == TASK_RUNNING)
258 return 0;
259
260 stack_limit = (unsigned long) (p + 1);
261 fp = p->thread.fp;
262 regs0 = p->thread.frame0;
263
264 do {
265 if (fp < stack_limit || fp >= (unsigned long) regs0 || fp & 3)
266 return 0;
267
268 pc = ((unsigned long *) fp)[2];
269
270 /* FIXME: This depends on the order of these functions. */
271 if (!in_sched_functions(pc))
272 return pc;
273
274 fp = *(unsigned long *) fp;
275 } while (count++ < 16);
276
277 return 0;
278}
279
280unsigned long thread_saved_pc(struct task_struct *tsk)
281{
282 /* Check whether the thread is blocked in resume() */
283 if (in_sched_functions(tsk->thread.pc))
284 return ((unsigned long *)tsk->thread.fp)[2];
285 else
286 return tsk->thread.pc;
287}
288
289int elf_check_arch(const struct elf32_hdr *hdr)
290{
291 unsigned long hsr0 = __get_HSR(0);
292 unsigned long psr = __get_PSR();
293
294 if (hdr->e_machine != EM_FRV)
295 return 0;
296
297 switch (hdr->e_flags & EF_FRV_GPR_MASK) {
298 case EF_FRV_GPR64:
299 if ((hsr0 & HSR0_GRN) == HSR0_GRN_32)
300 return 0;
301 case EF_FRV_GPR32:
302 case 0:
303 break;
304 default:
305 return 0;
306 }
307
308 switch (hdr->e_flags & EF_FRV_FPR_MASK) {
309 case EF_FRV_FPR64:
310 if ((hsr0 & HSR0_FRN) == HSR0_FRN_32)
311 return 0;
312 case EF_FRV_FPR32:
313 case EF_FRV_FPR_NONE:
314 case 0:
315 break;
316 default:
317 return 0;
318 }
319
320 if ((hdr->e_flags & EF_FRV_MULADD) == EF_FRV_MULADD)
321 if (PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
322 PSR_IMPLE(psr) != PSR_IMPLE_FR451)
323 return 0;
324
325 switch (hdr->e_flags & EF_FRV_CPU_MASK) {
326 case EF_FRV_CPU_GENERIC:
327 break;
328 case EF_FRV_CPU_FR300:
329 case EF_FRV_CPU_SIMPLE:
330 case EF_FRV_CPU_TOMCAT:
331 default:
332 return 0;
333 case EF_FRV_CPU_FR400:
334 if (PSR_IMPLE(psr) != PSR_IMPLE_FR401 &&
335 PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
336 PSR_IMPLE(psr) != PSR_IMPLE_FR451 &&
337 PSR_IMPLE(psr) != PSR_IMPLE_FR551)
338 return 0;
339 break;
340 case EF_FRV_CPU_FR450:
341 if (PSR_IMPLE(psr) != PSR_IMPLE_FR451)
342 return 0;
343 break;
344 case EF_FRV_CPU_FR500:
345 if (PSR_IMPLE(psr) != PSR_IMPLE_FR501)
346 return 0;
347 break;
348 case EF_FRV_CPU_FR550:
349 if (PSR_IMPLE(psr) != PSR_IMPLE_FR551)
350 return 0;
351 break;
352 }
353
354 return 1;
355}
David Howells6d8c4e32006-07-10 04:44:55 -0700356
357int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
358{
359 memcpy(fpregs,
360 &current->thread.user->f,
361 sizeof(current->thread.user->f));
362 return 1;
363}