blob: d4de48bd5efe4211990ec7530e25ad95e97819ad [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);
40
41#include <asm/pgalloc.h>
42
David Howells9dec17e2006-07-10 04:44:51 -070043void (*pm_power_off)(void);
44EXPORT_SYMBOL(pm_power_off);
45
Eric Dumazet504f52b2011-03-22 16:30:41 -070046struct task_struct *alloc_task_struct_node(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Eric Dumazet504f52b2011-03-22 16:30:41 -070048 struct task_struct *p = kmalloc_node(THREAD_SIZE, GFP_KERNEL, node);
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 if (p)
51 atomic_set((atomic_t *)(p+1), 1);
52 return p;
53}
54
55void free_task_struct(struct task_struct *p)
56{
57 if (atomic_dec_and_test((atomic_t *)(p+1)))
58 kfree(p);
59}
60
61static void core_sleep_idle(void)
62{
63#ifdef LED_DEBUG_SLEEP
64 /* Show that we're sleeping... */
65 __set_LEDS(0x55aa);
66#endif
67 frv_cpu_core_sleep();
68#ifdef LED_DEBUG_SLEEP
69 /* ... and that we woke up */
70 __set_LEDS(0);
71#endif
72 mb();
73}
74
75void (*idle)(void) = core_sleep_idle;
76
77/*
78 * The idle thread. There's no useful work to be
79 * done, so just try to conserve power and have a
80 * low exit latency (ie sit in a loop waiting for
81 * somebody to say that they'd like to reschedule)
82 */
83void cpu_idle(void)
84{
85 /* endless idle loop with no priority at all */
86 while (1) {
87 while (!need_resched()) {
Christoph Lameter8defab32007-05-09 02:32:48 -070088 check_pgt_cache();
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (!frv_dma_inprogress && idle)
91 idle();
92 }
93
Thomas Gleixnerbd2f5532011-03-21 12:33:18 +010094 schedule_preempt_disabled();
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
96}
97
98void machine_restart(char * __unused)
99{
100 unsigned long reset_addr;
101#ifdef CONFIG_GDBSTUB
102 gdbstub_exit(0);
103#endif
104
105 if (PSR_IMPLE(__get_PSR()) == PSR_IMPLE_FR551)
106 reset_addr = 0xfefff500;
107 else
108 reset_addr = 0xfeff0500;
109
110 /* Software reset. */
111 asm volatile(" dcef @(gr0,gr0),1 ! membar !"
112 " sti %1,@(%0,0) !"
113 " nop ! nop ! nop ! nop ! nop ! "
114 " nop ! nop ! nop ! nop ! nop ! "
115 " nop ! nop ! nop ! nop ! nop ! "
116 " nop ! nop ! nop ! nop ! nop ! "
117 : : "r" (reset_addr), "r" (1) );
118
119 for (;;)
120 ;
121}
122
123void machine_halt(void)
124{
125#ifdef CONFIG_GDBSTUB
126 gdbstub_exit(0);
127#endif
128
129 for (;;);
130}
131
132void machine_power_off(void)
133{
134#ifdef CONFIG_GDBSTUB
135 gdbstub_exit(0);
136#endif
137
138 for (;;);
139}
140
141void flush_thread(void)
142{
Mathias Krauseadc400f2011-07-26 16:08:15 -0700143 /* nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146inline unsigned long user_stack(const struct pt_regs *regs)
147{
148 while (regs->next_frame)
149 regs = regs->next_frame;
150 return user_mode(regs) ? regs->sp : 0;
151}
152
153asmlinkage int sys_fork(void)
154{
155#ifndef CONFIG_MMU
156 /* fork almost works, enough to trick you into looking elsewhere:-( */
157 return -EINVAL;
158#else
159 return do_fork(SIGCHLD, user_stack(__frame), __frame, 0, NULL, NULL);
160#endif
161}
162
163asmlinkage int sys_vfork(void)
164{
165 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, user_stack(__frame), __frame, 0,
166 NULL, NULL);
167}
168
169/*****************************************************************************/
170/*
171 * clone a process
172 * - tlsptr is retrieved by copy_thread()
173 */
174asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
175 int __user *parent_tidptr, int __user *child_tidptr,
176 int __user *tlsptr)
177{
178 if (!newsp)
179 newsp = user_stack(__frame);
180 return do_fork(clone_flags, newsp, __frame, 0, parent_tidptr, child_tidptr);
181} /* end sys_clone() */
182
183/*****************************************************************************/
184/*
185 * This gets called before we allocate a new thread and copy
186 * the current task into it.
187 */
188void prepare_to_copy(struct task_struct *tsk)
189{
190 //unlazy_fpu(tsk);
191} /* end prepare_to_copy() */
192
193/*****************************************************************************/
194/*
195 * set up the kernel stack and exception frames for a new process
196 */
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700197int copy_thread(unsigned long clone_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 unsigned long usp, unsigned long topstk,
199 struct task_struct *p, struct pt_regs *regs)
200{
201 struct pt_regs *childregs0, *childregs, *regs0;
202
203 regs0 = __kernel_frame0_ptr;
204 childregs0 = (struct pt_regs *)
David Howells84e8cd62006-07-10 04:44:55 -0700205 (task_stack_page(p) + THREAD_SIZE - FRV_FRAME0_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 childregs = childregs0;
207
208 /* set up the userspace frame (the only place that the USP is stored) */
209 *childregs0 = *regs0;
210
211 childregs0->gr8 = 0;
212 childregs0->sp = usp;
213 childregs0->next_frame = NULL;
214
215 /* set up the return kernel frame if called from kernel_thread() */
216 if (regs != regs0) {
217 childregs--;
218 *childregs = *regs;
219 childregs->sp = (unsigned long) childregs0;
220 childregs->next_frame = childregs0;
Al Viro097cb332006-01-12 01:05:53 -0800221 childregs->gr15 = (unsigned long) task_thread_info(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 childregs->gr29 = (unsigned long) p;
223 }
224
225 p->set_child_tid = p->clear_child_tid = NULL;
226
227 p->thread.frame = childregs;
228 p->thread.curr = p;
229 p->thread.sp = (unsigned long) childregs;
230 p->thread.fp = 0;
231 p->thread.lr = 0;
232 p->thread.pc = (unsigned long) ret_from_fork;
233 p->thread.frame0 = childregs0;
234
235 /* the new TLS pointer is passed in as arg #5 to sys_clone() */
236 if (clone_flags & CLONE_SETTLS)
237 childregs->gr29 = childregs->gr12;
238
239 save_user_regs(p->thread.user);
240
241 return 0;
242} /* end copy_thread() */
243
244/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 * sys_execve() executes a new program.
246 */
David Howellsd7627462010-08-17 23:52:56 +0100247asmlinkage int sys_execve(const char __user *name,
248 const char __user *const __user *argv,
249 const char __user *const __user *envp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 int error;
252 char * filename;
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 filename = getname(name);
255 error = PTR_ERR(filename);
256 if (IS_ERR(filename))
John Kacurb69975a2009-10-12 22:53:25 +0200257 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 error = do_execve(filename, argv, envp, __frame);
259 putname(filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return error;
261}
262
263unsigned long get_wchan(struct task_struct *p)
264{
265 struct pt_regs *regs0;
266 unsigned long fp, pc;
267 unsigned long stack_limit;
268 int count = 0;
269 if (!p || p == current || p->state == TASK_RUNNING)
270 return 0;
271
272 stack_limit = (unsigned long) (p + 1);
273 fp = p->thread.fp;
274 regs0 = p->thread.frame0;
275
276 do {
277 if (fp < stack_limit || fp >= (unsigned long) regs0 || fp & 3)
278 return 0;
279
280 pc = ((unsigned long *) fp)[2];
281
282 /* FIXME: This depends on the order of these functions. */
283 if (!in_sched_functions(pc))
284 return pc;
285
286 fp = *(unsigned long *) fp;
287 } while (count++ < 16);
288
289 return 0;
290}
291
292unsigned long thread_saved_pc(struct task_struct *tsk)
293{
294 /* Check whether the thread is blocked in resume() */
295 if (in_sched_functions(tsk->thread.pc))
296 return ((unsigned long *)tsk->thread.fp)[2];
297 else
298 return tsk->thread.pc;
299}
300
301int elf_check_arch(const struct elf32_hdr *hdr)
302{
303 unsigned long hsr0 = __get_HSR(0);
304 unsigned long psr = __get_PSR();
305
306 if (hdr->e_machine != EM_FRV)
307 return 0;
308
309 switch (hdr->e_flags & EF_FRV_GPR_MASK) {
310 case EF_FRV_GPR64:
311 if ((hsr0 & HSR0_GRN) == HSR0_GRN_32)
312 return 0;
313 case EF_FRV_GPR32:
314 case 0:
315 break;
316 default:
317 return 0;
318 }
319
320 switch (hdr->e_flags & EF_FRV_FPR_MASK) {
321 case EF_FRV_FPR64:
322 if ((hsr0 & HSR0_FRN) == HSR0_FRN_32)
323 return 0;
324 case EF_FRV_FPR32:
325 case EF_FRV_FPR_NONE:
326 case 0:
327 break;
328 default:
329 return 0;
330 }
331
332 if ((hdr->e_flags & EF_FRV_MULADD) == EF_FRV_MULADD)
333 if (PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
334 PSR_IMPLE(psr) != PSR_IMPLE_FR451)
335 return 0;
336
337 switch (hdr->e_flags & EF_FRV_CPU_MASK) {
338 case EF_FRV_CPU_GENERIC:
339 break;
340 case EF_FRV_CPU_FR300:
341 case EF_FRV_CPU_SIMPLE:
342 case EF_FRV_CPU_TOMCAT:
343 default:
344 return 0;
345 case EF_FRV_CPU_FR400:
346 if (PSR_IMPLE(psr) != PSR_IMPLE_FR401 &&
347 PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
348 PSR_IMPLE(psr) != PSR_IMPLE_FR451 &&
349 PSR_IMPLE(psr) != PSR_IMPLE_FR551)
350 return 0;
351 break;
352 case EF_FRV_CPU_FR450:
353 if (PSR_IMPLE(psr) != PSR_IMPLE_FR451)
354 return 0;
355 break;
356 case EF_FRV_CPU_FR500:
357 if (PSR_IMPLE(psr) != PSR_IMPLE_FR501)
358 return 0;
359 break;
360 case EF_FRV_CPU_FR550:
361 if (PSR_IMPLE(psr) != PSR_IMPLE_FR551)
362 return 0;
363 break;
364 }
365
366 return 1;
367}
David Howells6d8c4e32006-07-10 04:44:55 -0700368
369int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
370{
371 memcpy(fpregs,
372 &current->thread.user->f,
373 sizeof(current->thread.user->f));
374 return 1;
375}