Merge branch 'master' of /home/sam/kernel/linux-2.6/
[pandora-kernel.git] / arch / cris / arch-v32 / kernel / process.c
1 /*
2  *  Copyright (C) 2000-2003  Axis Communications AB
3  *
4  *  Authors:   Bjorn Wesen (bjornw@axis.com)
5  *             Mikael Starvik (starvik@axis.com)
6  *             Tobias Anderberg (tobiasa@axis.com), CRISv32 port.
7  *
8  * This file handles the architecture-dependent parts of process handling..
9  */
10
11 #include <linux/sched.h>
12 #include <linux/err.h>
13 #include <linux/fs.h>
14 #include <linux/slab.h>
15 #include <asm/arch/hwregs/reg_rdwr.h>
16 #include <asm/arch/hwregs/reg_map.h>
17 #include <asm/arch/hwregs/timer_defs.h>
18 #include <asm/arch/hwregs/intr_vect_defs.h>
19
20 extern void stop_watchdog(void);
21
22 #ifdef CONFIG_ETRAX_GPIO
23 extern void etrax_gpio_wake_up_check(void); /* Defined in drivers/gpio.c. */
24 #endif
25
26 extern int cris_hlt_counter;
27
28 /* We use this if we don't have any better idle routine. */
29 void default_idle(void)
30 {
31         local_irq_disable();
32         if (!need_resched() && !cris_hlt_counter) {
33                 /* Halt until exception. */
34                 __asm__ volatile("ei    \n\t"
35                                  "halt      ");
36         }
37         local_irq_enable();
38 }
39
40 /*
41  * Free current thread data structures etc..
42  */
43
44 extern void deconfigure_bp(long pid);
45 void exit_thread(void)
46 {
47         deconfigure_bp(current->pid);
48 }
49
50 /*
51  * If the watchdog is enabled, disable interrupts and enter an infinite loop.
52  * The watchdog will reset the CPU after 0.1s. If the watchdog isn't enabled
53  * then enable it and wait.
54  */
55 extern void arch_enable_nmi(void);
56
57 void
58 hard_reset_now(void)
59 {
60         /*
61          * Don't declare this variable elsewhere.  We don't want any other
62          * code to know about it than the watchdog handler in entry.S and
63          * this code, implementing hard reset through the watchdog.
64          */
65 #if defined(CONFIG_ETRAX_WATCHDOG)
66         extern int cause_of_death;
67 #endif
68
69         printk("*** HARD RESET ***\n");
70         local_irq_disable();
71
72 #if defined(CONFIG_ETRAX_WATCHDOG)
73         cause_of_death = 0xbedead;
74 #else
75 {
76         reg_timer_rw_wd_ctrl wd_ctrl = {0};
77
78         stop_watchdog();
79
80         wd_ctrl.key = 16;       /* Arbitrary key. */
81         wd_ctrl.cnt = 1;        /* Minimum time. */
82         wd_ctrl.cmd = regk_timer_start;
83
84         arch_enable_nmi();
85         REG_WR(timer, regi_timer, rw_wd_ctrl, wd_ctrl);
86 }
87 #endif
88
89         while (1)
90                 ; /* Wait for reset. */
91 }
92
93 /*
94  * Return saved PC of a blocked thread.
95  */
96 unsigned long thread_saved_pc(struct task_struct *t)
97 {
98         return task_pt_regs(t)->erp;
99 }
100
101 static void
102 kernel_thread_helper(void* dummy, int (*fn)(void *), void * arg)
103 {
104         fn(arg);
105         do_exit(-1); /* Should never be called, return bad exit value. */
106 }
107
108 /* Create a kernel thread. */
109 int
110 kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
111 {
112         struct pt_regs regs;
113
114         memset(&regs, 0, sizeof(regs));
115
116         /* Don't use r10 since that is set to 0 in copy_thread. */
117         regs.r11 = (unsigned long) fn;
118         regs.r12 = (unsigned long) arg;
119         regs.erp = (unsigned long) kernel_thread_helper;
120         regs.ccs = 1 << (I_CCS_BITNR + CCS_SHIFT);
121
122         /* Create the new process. */
123         return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
124 }
125
126 /*
127  * Setup the child's kernel stack with a pt_regs and call switch_stack() on it.
128  * It will be unnested during _resume and _ret_from_sys_call when the new thread
129  * is scheduled.
130  *
131  * Also setup the thread switching structure which is used to keep
132  * thread-specific data during _resumes.
133  */
134
135 extern asmlinkage void ret_from_fork(void);
136
137 int
138 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
139         unsigned long unused,
140         struct task_struct *p, struct pt_regs *regs)
141 {
142         struct pt_regs *childregs;
143         struct switch_stack *swstack;
144
145         /*
146          * Put the pt_regs structure at the end of the new kernel stack page and
147          * fix it up. Note: the task_struct doubles as the kernel stack for the
148          * task.
149          */
150         childregs = task_pt_regs(p);
151         *childregs = *regs;     /* Struct copy of pt_regs. */
152         p->set_child_tid = p->clear_child_tid = NULL;
153         childregs->r10 = 0;     /* Child returns 0 after a fork/clone. */
154
155         /* Set a new TLS ?
156          * The TLS is in $mof beacuse it is the 5th argument to sys_clone.
157          */
158         if (p->mm && (clone_flags & CLONE_SETTLS)) {
159                 task_thread_info(p)->tls = regs->mof;
160         }
161
162         /* Put the switch stack right below the pt_regs. */
163         swstack = ((struct switch_stack *) childregs) - 1;
164
165         /* Paramater to ret_from_sys_call. 0 is don't restart the syscall. */
166         swstack->r9 = 0;
167
168         /*
169          * We want to return into ret_from_sys_call after the _resume.
170          * ret_from_fork will call ret_from_sys_call.
171          */
172         swstack->return_ip = (unsigned long) ret_from_fork;
173
174         /* Fix the user-mode and kernel-mode stackpointer. */
175         p->thread.usp = usp;
176         p->thread.ksp = (unsigned long) swstack;
177
178         return 0;
179 }
180
181 /*
182  * Be aware of the "magic" 7th argument in the four system-calls below.
183  * They need the latest stackframe, which is put as the 7th argument by
184  * entry.S. The previous arguments are dummies or actually used, but need
185  * to be defined to reach the 7th argument.
186  *
187  * N.B.: Another method to get the stackframe is to use current_regs(). But
188  * it returns the latest stack-frame stacked when going from _user mode_ and
189  * some of these (at least sys_clone) are called from kernel-mode sometimes
190  * (for example during kernel_thread, above) and thus cannot use it. Thus,
191  * to be sure not to get any surprises, we use the method for the other calls
192  * as well.
193  */
194 asmlinkage int
195 sys_fork(long r10, long r11, long r12, long r13, long mof, long srp,
196         struct pt_regs *regs)
197 {
198         return do_fork(SIGCHLD, rdusp(), regs, 0, NULL, NULL);
199 }
200
201 /* FIXME: Is parent_tid/child_tid really third/fourth argument? Update lib? */
202 asmlinkage int
203 sys_clone(unsigned long newusp, unsigned long flags, int *parent_tid, int *child_tid,
204         unsigned long tls, long srp, struct pt_regs *regs)
205 {
206         if (!newusp)
207                 newusp = rdusp();
208
209         return do_fork(flags, newusp, regs, 0, parent_tid, child_tid);
210 }
211
212 /*
213  * vfork is a system call in i386 because of register-pressure - maybe
214  * we can remove it and handle it in libc but we put it here until then.
215  */
216 asmlinkage int
217 sys_vfork(long r10, long r11, long r12, long r13, long mof, long srp,
218         struct pt_regs *regs)
219 {
220         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL, NULL);
221 }
222
223 /* sys_execve() executes a new program. */
224 asmlinkage int
225 sys_execve(const char *fname, char **argv, char **envp, long r13, long mof, long srp,
226         struct pt_regs *regs)
227 {
228         int error;
229         char *filename;
230
231         filename = getname(fname);
232         error = PTR_ERR(filename);
233
234         if (IS_ERR(filename))
235                 goto out;
236
237         error = do_execve(filename, argv, envp, regs);
238         putname(filename);
239  out:
240         return error;
241 }
242
243 unsigned long
244 get_wchan(struct task_struct *p)
245 {
246         /* TODO */
247         return 0;
248 }
249 #undef last_sched
250 #undef first_sched
251
252 void show_regs(struct pt_regs * regs)
253 {
254         unsigned long usp = rdusp();
255         printk("ERP: %08lx SRP: %08lx  CCS: %08lx USP: %08lx MOF: %08lx\n",
256                 regs->erp, regs->srp, regs->ccs, usp, regs->mof);
257
258         printk(" r0: %08lx  r1: %08lx   r2: %08lx  r3: %08lx\n",
259                 regs->r0, regs->r1, regs->r2, regs->r3);
260
261         printk(" r4: %08lx  r5: %08lx   r6: %08lx  r7: %08lx\n",
262                 regs->r4, regs->r5, regs->r6, regs->r7);
263
264         printk(" r8: %08lx  r9: %08lx  r10: %08lx r11: %08lx\n",
265                 regs->r8, regs->r9, regs->r10, regs->r11);
266
267         printk("r12: %08lx r13: %08lx oR10: %08lx\n",
268                 regs->r12, regs->r13, regs->orig_r10);
269 }