1 /* process.c: FRV specific parts of process handling
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
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.
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
17 #include <linux/smp.h>
18 #include <linux/smp_lock.h>
19 #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>
28 #include <asm/uaccess.h>
29 #include <asm/system.h>
30 #include <asm/setup.h>
31 #include <asm/pgtable.h>
32 #include <asm/gdb-stub.h>
33 #include <asm/mb-regs.h>
37 asmlinkage void ret_from_fork(void);
39 #include <asm/pgalloc.h>
41 struct task_struct *alloc_task_struct(void)
43 struct task_struct *p = kmalloc(THREAD_SIZE, GFP_KERNEL);
45 atomic_set((atomic_t *)(p+1), 1);
49 void free_task_struct(struct task_struct *p)
51 if (atomic_dec_and_test((atomic_t *)(p+1)))
55 static void core_sleep_idle(void)
57 #ifdef LED_DEBUG_SLEEP
58 /* Show that we're sleeping... */
62 #ifdef LED_DEBUG_SLEEP
63 /* ... and that we woke up */
69 void (*idle)(void) = core_sleep_idle;
72 * The idle thread. There's no useful work to be
73 * done, so just try to conserve power and have a
74 * low exit latency (ie sit in a loop waiting for
75 * somebody to say that they'd like to reschedule)
79 int cpu = smp_processor_id();
81 /* endless idle loop with no priority at all */
83 while (!need_resched()) {
84 irq_stat[cpu].idle_timestamp = jiffies;
86 if (!frv_dma_inprogress && idle)
90 preempt_enable_no_resched();
96 void machine_restart(char * __unused)
98 unsigned long reset_addr;
103 if (PSR_IMPLE(__get_PSR()) == PSR_IMPLE_FR551)
104 reset_addr = 0xfefff500;
106 reset_addr = 0xfeff0500;
108 /* Software reset. */
109 asm volatile(" dcef @(gr0,gr0),1 ! membar !"
111 " nop ! nop ! nop ! nop ! nop ! "
112 " nop ! nop ! nop ! nop ! nop ! "
113 " nop ! nop ! nop ! nop ! nop ! "
114 " nop ! nop ! nop ! nop ! nop ! "
115 : : "r" (reset_addr), "r" (1) );
121 void machine_halt(void)
123 #ifdef CONFIG_GDBSTUB
130 void machine_power_off(void)
132 #ifdef CONFIG_GDBSTUB
139 void flush_thread(void)
142 unsigned long zero = 0;
147 inline unsigned long user_stack(const struct pt_regs *regs)
149 while (regs->next_frame)
150 regs = regs->next_frame;
151 return user_mode(regs) ? regs->sp : 0;
154 asmlinkage int sys_fork(void)
157 /* fork almost works, enough to trick you into looking elsewhere:-( */
160 return do_fork(SIGCHLD, user_stack(__frame), __frame, 0, NULL, NULL);
164 asmlinkage int sys_vfork(void)
166 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, user_stack(__frame), __frame, 0,
170 /*****************************************************************************/
173 * - tlsptr is retrieved by copy_thread()
175 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
176 int __user *parent_tidptr, int __user *child_tidptr,
180 newsp = user_stack(__frame);
181 return do_fork(clone_flags, newsp, __frame, 0, parent_tidptr, child_tidptr);
182 } /* end sys_clone() */
184 /*****************************************************************************/
186 * This gets called before we allocate a new thread and copy
187 * the current task into it.
189 void prepare_to_copy(struct task_struct *tsk)
192 } /* end prepare_to_copy() */
194 /*****************************************************************************/
196 * set up the kernel stack and exception frames for a new process
198 int copy_thread(int nr, unsigned long clone_flags,
199 unsigned long usp, unsigned long topstk,
200 struct task_struct *p, struct pt_regs *regs)
202 struct pt_regs *childregs0, *childregs, *regs0;
204 regs0 = __kernel_frame0_ptr;
205 childregs0 = (struct pt_regs *)
206 (task_stack_page(p) + THREAD_SIZE - USER_CONTEXT_SIZE);
207 childregs = childregs0;
209 /* set up the userspace frame (the only place that the USP is stored) */
210 *childregs0 = *regs0;
213 childregs0->sp = usp;
214 childregs0->next_frame = NULL;
216 /* set up the return kernel frame if called from kernel_thread() */
220 childregs->sp = (unsigned long) childregs0;
221 childregs->next_frame = childregs0;
222 childregs->gr15 = (unsigned long) task_thread_info(p);
223 childregs->gr29 = (unsigned long) p;
226 p->set_child_tid = p->clear_child_tid = NULL;
228 p->thread.frame = childregs;
230 p->thread.sp = (unsigned long) childregs;
233 p->thread.pc = (unsigned long) ret_from_fork;
234 p->thread.frame0 = childregs0;
236 /* the new TLS pointer is passed in as arg #5 to sys_clone() */
237 if (clone_flags & CLONE_SETTLS)
238 childregs->gr29 = childregs->gr12;
240 save_user_regs(p->thread.user);
243 } /* end copy_thread() */
246 * sys_execve() executes a new program.
248 asmlinkage int sys_execve(char __user *name, char __user * __user *argv, char __user * __user *envp)
254 filename = getname(name);
255 error = PTR_ERR(filename);
256 if (IS_ERR(filename))
258 error = do_execve(filename, argv, envp, __frame);
265 unsigned long get_wchan(struct task_struct *p)
267 struct pt_regs *regs0;
268 unsigned long fp, pc;
269 unsigned long stack_limit;
271 if (!p || p == current || p->state == TASK_RUNNING)
274 stack_limit = (unsigned long) (p + 1);
276 regs0 = p->thread.frame0;
279 if (fp < stack_limit || fp >= (unsigned long) regs0 || fp & 3)
282 pc = ((unsigned long *) fp)[2];
284 /* FIXME: This depends on the order of these functions. */
285 if (!in_sched_functions(pc))
288 fp = *(unsigned long *) fp;
289 } while (count++ < 16);
294 unsigned long thread_saved_pc(struct task_struct *tsk)
296 /* Check whether the thread is blocked in resume() */
297 if (in_sched_functions(tsk->thread.pc))
298 return ((unsigned long *)tsk->thread.fp)[2];
300 return tsk->thread.pc;
303 int elf_check_arch(const struct elf32_hdr *hdr)
305 unsigned long hsr0 = __get_HSR(0);
306 unsigned long psr = __get_PSR();
308 if (hdr->e_machine != EM_FRV)
311 switch (hdr->e_flags & EF_FRV_GPR_MASK) {
313 if ((hsr0 & HSR0_GRN) == HSR0_GRN_32)
322 switch (hdr->e_flags & EF_FRV_FPR_MASK) {
324 if ((hsr0 & HSR0_FRN) == HSR0_FRN_32)
327 case EF_FRV_FPR_NONE:
334 if ((hdr->e_flags & EF_FRV_MULADD) == EF_FRV_MULADD)
335 if (PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
336 PSR_IMPLE(psr) != PSR_IMPLE_FR451)
339 switch (hdr->e_flags & EF_FRV_CPU_MASK) {
340 case EF_FRV_CPU_GENERIC:
342 case EF_FRV_CPU_FR300:
343 case EF_FRV_CPU_SIMPLE:
344 case EF_FRV_CPU_TOMCAT:
347 case EF_FRV_CPU_FR400:
348 if (PSR_IMPLE(psr) != PSR_IMPLE_FR401 &&
349 PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
350 PSR_IMPLE(psr) != PSR_IMPLE_FR451 &&
351 PSR_IMPLE(psr) != PSR_IMPLE_FR551)
354 case EF_FRV_CPU_FR450:
355 if (PSR_IMPLE(psr) != PSR_IMPLE_FR451)
358 case EF_FRV_CPU_FR500:
359 if (PSR_IMPLE(psr) != PSR_IMPLE_FR501)
362 case EF_FRV_CPU_FR550:
363 if (PSR_IMPLE(psr) != PSR_IMPLE_FR551)