blob: 0f02dee25e262c83460fdbf354a9e26409e8ab6f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
David Howells84e8cd62006-07-10 04:44:55 -070029#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/setup.h>
32#include <asm/pgtable.h>
Christoph Lameter8defab32007-05-09 02:32:48 -070033#include <asm/tlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/gdb-stub.h>
35#include <asm/mb-regs.h>
36
37#include "local.h"
38
39asmlinkage void ret_from_fork(void);
Al Viro02ce4962012-09-18 22:18:51 -040040asmlinkage void ret_from_kernel_thread(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
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) {
73 while (!need_resched()) {
Christoph Lameter8defab32007-05-09 02:32:48 -070074 check_pgt_cache();
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (!frv_dma_inprogress && idle)
77 idle();
78 }
79
Thomas Gleixnerbd2f5532011-03-21 12:33:18 +010080 schedule_preempt_disabled();
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82}
83
84void 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
109void machine_halt(void)
110{
111#ifdef CONFIG_GDBSTUB
112 gdbstub_exit(0);
113#endif
114
115 for (;;);
116}
117
118void machine_power_off(void)
119{
120#ifdef CONFIG_GDBSTUB
121 gdbstub_exit(0);
122#endif
123
124 for (;;);
125}
126
127void flush_thread(void)
128{
Mathias Krauseadc400f2011-07-26 16:08:15 -0700129 /* nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132inline 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
139asmlinkage 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
149asmlinkage 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 */
160asmlinkage 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/*
170 * set up the kernel stack and exception frames for a new process
171 */
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700172int copy_thread(unsigned long clone_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 unsigned long usp, unsigned long topstk,
174 struct task_struct *p, struct pt_regs *regs)
175{
Al Viro02ce4962012-09-18 22:18:51 -0400176 struct pt_regs *childregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Al Viro02ce4962012-09-18 22:18:51 -0400178 childregs = (struct pt_regs *)
David Howells84e8cd62006-07-10 04:44:55 -0700179 (task_stack_page(p) + THREAD_SIZE - FRV_FRAME0_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /* set up the userspace frame (the only place that the USP is stored) */
Al Viro02ce4962012-09-18 22:18:51 -0400182 *childregs = *regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Al Viro02ce4962012-09-18 22:18:51 -0400184 childregs->sp = usp;
185 childregs->next_frame = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Al Viro02ce4962012-09-18 22:18:51 -0400187 if (unlikely(!user_mode(regs)))
188 p->thread.pc = (unsigned long) ret_from_kernel_thread;
189 else
190 p->thread.pc = (unsigned long) ret_from_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 p->set_child_tid = p->clear_child_tid = NULL;
193
194 p->thread.frame = childregs;
195 p->thread.curr = p;
196 p->thread.sp = (unsigned long) childregs;
197 p->thread.fp = 0;
198 p->thread.lr = 0;
Al Viro02ce4962012-09-18 22:18:51 -0400199 p->thread.frame0 = childregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 /* the new TLS pointer is passed in as arg #5 to sys_clone() */
202 if (clone_flags & CLONE_SETTLS)
203 childregs->gr29 = childregs->gr12;
204
205 save_user_regs(p->thread.user);
206
207 return 0;
208} /* end copy_thread() */
209
210/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * sys_execve() executes a new program.
212 */
David Howellsd7627462010-08-17 23:52:56 +0100213asmlinkage int sys_execve(const char __user *name,
214 const char __user *const __user *argv,
215 const char __user *const __user *envp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 int error;
218 char * filename;
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 filename = getname(name);
221 error = PTR_ERR(filename);
222 if (IS_ERR(filename))
John Kacurb69975a2009-10-12 22:53:25 +0200223 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 error = do_execve(filename, argv, envp, __frame);
225 putname(filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return error;
227}
228
229unsigned long get_wchan(struct task_struct *p)
230{
231 struct pt_regs *regs0;
232 unsigned long fp, pc;
233 unsigned long stack_limit;
234 int count = 0;
235 if (!p || p == current || p->state == TASK_RUNNING)
236 return 0;
237
238 stack_limit = (unsigned long) (p + 1);
239 fp = p->thread.fp;
240 regs0 = p->thread.frame0;
241
242 do {
243 if (fp < stack_limit || fp >= (unsigned long) regs0 || fp & 3)
244 return 0;
245
246 pc = ((unsigned long *) fp)[2];
247
248 /* FIXME: This depends on the order of these functions. */
249 if (!in_sched_functions(pc))
250 return pc;
251
252 fp = *(unsigned long *) fp;
253 } while (count++ < 16);
254
255 return 0;
256}
257
258unsigned long thread_saved_pc(struct task_struct *tsk)
259{
260 /* Check whether the thread is blocked in resume() */
261 if (in_sched_functions(tsk->thread.pc))
262 return ((unsigned long *)tsk->thread.fp)[2];
263 else
264 return tsk->thread.pc;
265}
266
267int elf_check_arch(const struct elf32_hdr *hdr)
268{
269 unsigned long hsr0 = __get_HSR(0);
270 unsigned long psr = __get_PSR();
271
272 if (hdr->e_machine != EM_FRV)
273 return 0;
274
275 switch (hdr->e_flags & EF_FRV_GPR_MASK) {
276 case EF_FRV_GPR64:
277 if ((hsr0 & HSR0_GRN) == HSR0_GRN_32)
278 return 0;
279 case EF_FRV_GPR32:
280 case 0:
281 break;
282 default:
283 return 0;
284 }
285
286 switch (hdr->e_flags & EF_FRV_FPR_MASK) {
287 case EF_FRV_FPR64:
288 if ((hsr0 & HSR0_FRN) == HSR0_FRN_32)
289 return 0;
290 case EF_FRV_FPR32:
291 case EF_FRV_FPR_NONE:
292 case 0:
293 break;
294 default:
295 return 0;
296 }
297
298 if ((hdr->e_flags & EF_FRV_MULADD) == EF_FRV_MULADD)
299 if (PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
300 PSR_IMPLE(psr) != PSR_IMPLE_FR451)
301 return 0;
302
303 switch (hdr->e_flags & EF_FRV_CPU_MASK) {
304 case EF_FRV_CPU_GENERIC:
305 break;
306 case EF_FRV_CPU_FR300:
307 case EF_FRV_CPU_SIMPLE:
308 case EF_FRV_CPU_TOMCAT:
309 default:
310 return 0;
311 case EF_FRV_CPU_FR400:
312 if (PSR_IMPLE(psr) != PSR_IMPLE_FR401 &&
313 PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
314 PSR_IMPLE(psr) != PSR_IMPLE_FR451 &&
315 PSR_IMPLE(psr) != PSR_IMPLE_FR551)
316 return 0;
317 break;
318 case EF_FRV_CPU_FR450:
319 if (PSR_IMPLE(psr) != PSR_IMPLE_FR451)
320 return 0;
321 break;
322 case EF_FRV_CPU_FR500:
323 if (PSR_IMPLE(psr) != PSR_IMPLE_FR501)
324 return 0;
325 break;
326 case EF_FRV_CPU_FR550:
327 if (PSR_IMPLE(psr) != PSR_IMPLE_FR551)
328 return 0;
329 break;
330 }
331
332 return 1;
333}
David Howells6d8c4e32006-07-10 04:44:55 -0700334
335int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
336{
337 memcpy(fpregs,
338 &current->thread.user->f,
339 sizeof(current->thread.user->f));
340 return 1;
341}
Al Viro02ce4962012-09-18 22:18:51 -0400342
343int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
344{
345 struct pt_regs regs = {
346 .gr8 = (unsigned long)arg;
347 .gr9 = (unsigned long)fn;
348 .psr = PSR_S;
349 };
350 return do_fork(flags|CLONE_VM|CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
351}