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