blob: 69e28b4057e8bc00f7ea93f4b756a589f20fbd32 [file] [log] [blame]
Mikael Starvik7cf32ca2005-07-27 11:44:38 -07001/* $Id: process.c,v 1.12 2004/12/27 11:18:32 starvik Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * linux/arch/cris/kernel/process.c
4 *
5 * Copyright (C) 1995 Linus Torvalds
6 * Copyright (C) 2000-2002 Axis Communications AB
7 *
8 * Authors: Bjorn Wesen (bjornw@axis.com)
9 * Mikael Starvik (starvik@axis.com)
10 *
11 * This file handles the architecture-dependent parts of process handling..
12 */
13
14#include <linux/config.h>
15#include <linux/sched.h>
16#include <linux/err.h>
17#include <linux/fs.h>
18#include <linux/slab.h>
19#include <asm/arch/svinto.h>
20#include <linux/init.h>
21
22#ifdef CONFIG_ETRAX_GPIO
23void etrax_gpio_wake_up_check(void); /* drivers/gpio.c */
24#endif
25
26/*
27 * We use this if we don't have any better
28 * idle routine..
29 */
30void default_idle(void)
31{
32#ifdef CONFIG_ETRAX_GPIO
33 etrax_gpio_wake_up_check();
34#endif
35}
36
37/*
38 * Free current thread data structures etc..
39 */
40
41void exit_thread(void)
42{
43 /* Nothing needs to be done. */
44}
45
46/* if the watchdog is enabled, we can simply disable interrupts and go
47 * into an eternal loop, and the watchdog will reset the CPU after 0.1s
48 * if on the other hand the watchdog wasn't enabled, we just enable it and wait
49 */
50
51void hard_reset_now (void)
52{
53 /*
54 * Don't declare this variable elsewhere. We don't want any other
55 * code to know about it than the watchdog handler in entry.S and
56 * this code, implementing hard reset through the watchdog.
57 */
58#if defined(CONFIG_ETRAX_WATCHDOG) && !defined(CONFIG_SVINTO_SIM)
59 extern int cause_of_death;
60#endif
61
62 printk("*** HARD RESET ***\n");
63 local_irq_disable();
64
65#if defined(CONFIG_ETRAX_WATCHDOG) && !defined(CONFIG_SVINTO_SIM)
66 cause_of_death = 0xbedead;
67#else
68 /* Since we dont plan to keep on reseting the watchdog,
69 the key can be arbitrary hence three */
70 *R_WATCHDOG = IO_FIELD(R_WATCHDOG, key, 3) |
71 IO_STATE(R_WATCHDOG, enable, start);
72#endif
73
74 while(1) /* waiting for RETRIBUTION! */ ;
75}
76
77/*
78 * Return saved PC of a blocked thread.
79 */
80unsigned long thread_saved_pc(struct task_struct *t)
81{
82 return (unsigned long)user_regs(t->thread_info)->irp;
83}
84
85static void kernel_thread_helper(void* dummy, int (*fn)(void *), void * arg)
86{
87 fn(arg);
88 do_exit(-1); /* Should never be called, return bad exit value */
89}
90
91/*
92 * Create a kernel thread
93 */
94int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
95{
96 struct pt_regs regs;
97
98 memset(&regs, 0, sizeof(regs));
99
100 /* Don't use r10 since that is set to 0 in copy_thread */
101 regs.r11 = (unsigned long)fn;
102 regs.r12 = (unsigned long)arg;
103 regs.irp = (unsigned long)kernel_thread_helper;
Mikael Starvik7cf32ca2005-07-27 11:44:38 -0700104 regs.dccr = 1 << I_DCCR_BITNR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 /* Ok, create the new process.. */
107 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
108}
109
110/* setup the child's kernel stack with a pt_regs and switch_stack on it.
111 * it will be un-nested during _resume and _ret_from_sys_call when the
112 * new thread is scheduled.
113 *
114 * also setup the thread switching structure which is used to keep
115 * thread-specific data during _resumes.
116 *
117 */
118asmlinkage void ret_from_fork(void);
119
120int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
121 unsigned long unused,
122 struct task_struct *p, struct pt_regs *regs)
123{
124 struct pt_regs * childregs;
125 struct switch_stack *swstack;
126
127 /* put the pt_regs structure at the end of the new kernel stack page and fix it up
128 * remember that the task_struct doubles as the kernel stack for the task
129 */
130
131 childregs = user_regs(p->thread_info);
132
133 *childregs = *regs; /* struct copy of pt_regs */
134
135 p->set_child_tid = p->clear_child_tid = NULL;
136
137 childregs->r10 = 0; /* child returns 0 after a fork/clone */
138
139 /* put the switch stack right below the pt_regs */
140
141 swstack = ((struct switch_stack *)childregs) - 1;
142
143 swstack->r9 = 0; /* parameter to ret_from_sys_call, 0 == dont restart the syscall */
144
145 /* we want to return into ret_from_sys_call after the _resume */
146
147 swstack->return_ip = (unsigned long) ret_from_fork; /* Will call ret_from_sys_call */
148
149 /* fix the user-mode stackpointer */
150
151 p->thread.usp = usp;
152
153 /* and the kernel-mode one */
154
155 p->thread.ksp = (unsigned long) swstack;
156
157#ifdef DEBUG
158 printk("copy_thread: new regs at 0x%p, as shown below:\n", childregs);
159 show_registers(childregs);
160#endif
161
162 return 0;
163}
164
165/*
166 * Be aware of the "magic" 7th argument in the four system-calls below.
167 * They need the latest stackframe, which is put as the 7th argument by
168 * entry.S. The previous arguments are dummies or actually used, but need
169 * to be defined to reach the 7th argument.
170 *
171 * N.B.: Another method to get the stackframe is to use current_regs(). But
172 * it returns the latest stack-frame stacked when going from _user mode_ and
173 * some of these (at least sys_clone) are called from kernel-mode sometimes
174 * (for example during kernel_thread, above) and thus cannot use it. Thus,
175 * to be sure not to get any surprises, we use the method for the other calls
176 * as well.
177 */
178
179asmlinkage int sys_fork(long r10, long r11, long r12, long r13, long mof, long srp,
180 struct pt_regs *regs)
181{
182 return do_fork(SIGCHLD, rdusp(), regs, 0, NULL, NULL);
183}
184
185/* if newusp is 0, we just grab the old usp */
186/* FIXME: Is parent_tid/child_tid really third/fourth argument? Update lib? */
187asmlinkage int sys_clone(unsigned long newusp, unsigned long flags,
188 int* parent_tid, int* child_tid, long mof, long srp,
189 struct pt_regs *regs)
190{
191 if (!newusp)
192 newusp = rdusp();
193 return do_fork(flags, newusp, regs, 0, parent_tid, child_tid);
194}
195
196/* vfork is a system call in i386 because of register-pressure - maybe
197 * we can remove it and handle it in libc but we put it here until then.
198 */
199
200asmlinkage int sys_vfork(long r10, long r11, long r12, long r13, long mof, long srp,
201 struct pt_regs *regs)
202{
203 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL, NULL);
204}
205
206/*
207 * sys_execve() executes a new program.
208 */
209asmlinkage int sys_execve(const char *fname, char **argv, char **envp,
210 long r13, long mof, long srp,
211 struct pt_regs *regs)
212{
213 int error;
214 char *filename;
215
216 filename = getname(fname);
217 error = PTR_ERR(filename);
218
219 if (IS_ERR(filename))
220 goto out;
221 error = do_execve(filename, argv, envp, regs);
222 putname(filename);
223 out:
224 return error;
225}
226
227unsigned long get_wchan(struct task_struct *p)
228{
229#if 0
230 /* YURGH. TODO. */
231
232 unsigned long ebp, esp, eip;
233 unsigned long stack_page;
234 int count = 0;
235 if (!p || p == current || p->state == TASK_RUNNING)
236 return 0;
237 stack_page = (unsigned long)p;
238 esp = p->thread.esp;
239 if (!stack_page || esp < stack_page || esp > 8188+stack_page)
240 return 0;
241 /* include/asm-i386/system.h:switch_to() pushes ebp last. */
242 ebp = *(unsigned long *) esp;
243 do {
244 if (ebp < stack_page || ebp > 8184+stack_page)
245 return 0;
246 eip = *(unsigned long *) (ebp+4);
247 if (!in_sched_functions(eip))
248 return eip;
249 ebp = *(unsigned long *) ebp;
250 } while (count++ < 16);
251#endif
252 return 0;
253}
254#undef last_sched
255#undef first_sched
256
257void show_regs(struct pt_regs * regs)
258{
259 unsigned long usp = rdusp();
260 printk("IRP: %08lx SRP: %08lx DCCR: %08lx USP: %08lx MOF: %08lx\n",
261 regs->irp, regs->srp, regs->dccr, usp, regs->mof );
262 printk(" r0: %08lx r1: %08lx r2: %08lx r3: %08lx\n",
263 regs->r0, regs->r1, regs->r2, regs->r3);
264 printk(" r4: %08lx r5: %08lx r6: %08lx r7: %08lx\n",
265 regs->r4, regs->r5, regs->r6, regs->r7);
266 printk(" r8: %08lx r9: %08lx r10: %08lx r11: %08lx\n",
267 regs->r8, regs->r9, regs->r10, regs->r11);
268 printk("r12: %08lx r13: %08lx oR10: %08lx\n",
269 regs->r12, regs->r13, regs->orig_r10);
270}
271