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