blob: 014f855362b9634a38c9cb8cdbc42d64c866cd46 [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,
Al Viro49ed3392012-09-22 18:10:15 -0400173 unsigned long usp, unsigned long arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 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;
Al Viro02ce4962012-09-18 22:18:51 -0400188 p->thread.frame0 = childregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Al Viro49ed3392012-09-22 18:10:15 -0400190 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217unsigned 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
246unsigned 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
255int 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}
David Howells6d8c4e32006-07-10 04:44:55 -0700322
323int 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}