Merge tag 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jgarzik...
[cascardo/linux.git] / arch / cris / arch-v10 / kernel / process.c
1 /*
2  *  linux/arch/cris/kernel/process.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  *  Copyright (C) 2000-2002  Axis Communications AB
6  *
7  *  Authors:   Bjorn Wesen (bjornw@axis.com)
8  *             Mikael Starvik (starvik@axis.com)
9  *
10  * This file handles the architecture-dependent parts of process handling..
11  */
12
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/fs.h>
17 #include <arch/svinto.h>
18 #include <linux/init.h>
19 #include <arch/system.h>
20 #include <linux/ptrace.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 /*
38  * Free current thread data structures etc..
39  */
40
41 void 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
51 void 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 resetting 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  */
80 unsigned long thread_saved_pc(struct task_struct *t)
81 {
82         return task_pt_regs(t)->irp;
83 }
84
85 /* setup the child's kernel stack with a pt_regs and switch_stack on it.
86  * it will be un-nested during _resume and _ret_from_sys_call when the
87  * new thread is scheduled.
88  *
89  * also setup the thread switching structure which is used to keep
90  * thread-specific data during _resumes.
91  *
92  */
93 asmlinkage void ret_from_fork(void);
94 asmlinkage void ret_from_kernel_thread(void);
95
96 int copy_thread(unsigned long clone_flags, unsigned long usp,
97                 unsigned long arg, struct task_struct *p)
98 {
99         struct pt_regs *childregs = task_pt_regs(p);
100         struct switch_stack *swstack = ((struct switch_stack *)childregs) - 1;
101         
102         /* put the pt_regs structure at the end of the new kernel stack page and fix it up
103          * remember that the task_struct doubles as the kernel stack for the task
104          */
105
106         if (unlikely(p->flags & PF_KTHREAD)) {
107                 memset(swstack, 0,
108                         sizeof(struct switch_stack) + sizeof(struct pt_regs));
109                 swstack->r1 = usp;
110                 swstack->r2 = arg;
111                 childregs->dccr = 1 << I_DCCR_BITNR;
112                 swstack->return_ip = (unsigned long) ret_from_kernel_thread;
113                 p->thread.ksp = (unsigned long) swstack;
114                 p->thread.usp = 0;
115                 return 0;
116         }
117         *childregs = *current_pt_regs();  /* struct copy of pt_regs */
118
119         childregs->r10 = 0;  /* child returns 0 after a fork/clone */
120
121         /* put the switch stack right below the pt_regs */
122
123         swstack->r9 = 0; /* parameter to ret_from_sys_call, 0 == dont restart the syscall */
124
125         /* we want to return into ret_from_sys_call after the _resume */
126
127         swstack->return_ip = (unsigned long) ret_from_fork; /* Will call ret_from_sys_call */
128         
129         /* fix the user-mode stackpointer */
130
131         p->thread.usp = usp ?: rdusp();
132
133         /* and the kernel-mode one */
134
135         p->thread.ksp = (unsigned long) swstack;
136
137 #ifdef DEBUG
138         printk("copy_thread: new regs at 0x%p, as shown below:\n", childregs);
139         show_registers(childregs);
140 #endif
141
142         return 0;
143 }
144
145 unsigned long get_wchan(struct task_struct *p)
146 {
147 #if 0
148         /* YURGH. TODO. */
149
150         unsigned long ebp, esp, eip;
151         unsigned long stack_page;
152         int count = 0;
153         if (!p || p == current || p->state == TASK_RUNNING)
154                 return 0;
155         stack_page = (unsigned long)p;
156         esp = p->thread.esp;
157         if (!stack_page || esp < stack_page || esp > 8188+stack_page)
158                 return 0;
159         /* include/asm-i386/system.h:switch_to() pushes ebp last. */
160         ebp = *(unsigned long *) esp;
161         do {
162                 if (ebp < stack_page || ebp > 8184+stack_page)
163                         return 0;
164                 eip = *(unsigned long *) (ebp+4);
165                 if (!in_sched_functions(eip))
166                         return eip;
167                 ebp = *(unsigned long *) ebp;
168         } while (count++ < 16);
169 #endif
170         return 0;
171 }
172 #undef last_sched
173 #undef first_sched
174
175 void show_regs(struct pt_regs * regs)
176 {
177         unsigned long usp = rdusp();
178         printk("IRP: %08lx SRP: %08lx DCCR: %08lx USP: %08lx MOF: %08lx\n",
179                regs->irp, regs->srp, regs->dccr, usp, regs->mof );
180         printk(" r0: %08lx  r1: %08lx   r2: %08lx  r3: %08lx\n",
181                regs->r0, regs->r1, regs->r2, regs->r3);
182         printk(" r4: %08lx  r5: %08lx   r6: %08lx  r7: %08lx\n",
183                regs->r4, regs->r5, regs->r6, regs->r7);
184         printk(" r8: %08lx  r9: %08lx  r10: %08lx r11: %08lx\n",
185                regs->r8, regs->r9, regs->r10, regs->r11);
186         printk("r12: %08lx r13: %08lx oR10: %08lx\n",
187                regs->r12, regs->r13, regs->orig_r10);
188 }
189