Merge branch 'for-usb-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/sarah...
[pandora-kernel.git] / arch / frv / kernel / process.c
1 /* process.c: FRV specific parts of process handling
2  *
3  * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  * - Derived from arch/m68k/kernel/process.c
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version
10  * 2 of the License, or (at your option) any later version.
11  */
12
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/smp.h>
19 #include <linux/stddef.h>
20 #include <linux/unistd.h>
21 #include <linux/ptrace.h>
22 #include <linux/slab.h>
23 #include <linux/user.h>
24 #include <linux/elf.h>
25 #include <linux/reboot.h>
26 #include <linux/interrupt.h>
27 #include <linux/pagemap.h>
28
29 #include <asm/asm-offsets.h>
30 #include <asm/uaccess.h>
31 #include <asm/system.h>
32 #include <asm/setup.h>
33 #include <asm/pgtable.h>
34 #include <asm/tlb.h>
35 #include <asm/gdb-stub.h>
36 #include <asm/mb-regs.h>
37
38 #include "local.h"
39
40 asmlinkage void ret_from_fork(void);
41
42 #include <asm/pgalloc.h>
43
44 void (*pm_power_off)(void);
45 EXPORT_SYMBOL(pm_power_off);
46
47 struct task_struct *alloc_task_struct(void)
48 {
49         struct task_struct *p = kmalloc(THREAD_SIZE, GFP_KERNEL);
50         if (p)
51                 atomic_set((atomic_t *)(p+1), 1);
52         return p;
53 }
54
55 void free_task_struct(struct task_struct *p)
56 {
57         if (atomic_dec_and_test((atomic_t *)(p+1)))
58                 kfree(p);
59 }
60
61 static void core_sleep_idle(void)
62 {
63 #ifdef LED_DEBUG_SLEEP
64         /* Show that we're sleeping... */
65         __set_LEDS(0x55aa);
66 #endif
67         frv_cpu_core_sleep();
68 #ifdef LED_DEBUG_SLEEP
69         /* ... and that we woke up */
70         __set_LEDS(0);
71 #endif
72         mb();
73 }
74
75 void (*idle)(void) = core_sleep_idle;
76
77 /*
78  * The idle thread. There's no useful work to be
79  * done, so just try to conserve power and have a
80  * low exit latency (ie sit in a loop waiting for
81  * somebody to say that they'd like to reschedule)
82  */
83 void cpu_idle(void)
84 {
85         /* endless idle loop with no priority at all */
86         while (1) {
87                 while (!need_resched()) {
88                         check_pgt_cache();
89
90                         if (!frv_dma_inprogress && idle)
91                                 idle();
92                 }
93
94                 preempt_enable_no_resched();
95                 schedule();
96                 preempt_disable();
97         }
98 }
99
100 void machine_restart(char * __unused)
101 {
102         unsigned long reset_addr;
103 #ifdef CONFIG_GDBSTUB
104         gdbstub_exit(0);
105 #endif
106
107         if (PSR_IMPLE(__get_PSR()) == PSR_IMPLE_FR551)
108                 reset_addr = 0xfefff500;
109         else
110                 reset_addr = 0xfeff0500;
111
112         /* Software reset. */
113         asm volatile("      dcef @(gr0,gr0),1 ! membar !"
114                      "      sti     %1,@(%0,0) !"
115                      "      nop ! nop ! nop ! nop ! nop ! "
116                      "      nop ! nop ! nop ! nop ! nop ! "
117                      "      nop ! nop ! nop ! nop ! nop ! "
118                      "      nop ! nop ! nop ! nop ! nop ! "
119                      : : "r" (reset_addr), "r" (1) );
120
121         for (;;)
122                 ;
123 }
124
125 void machine_halt(void)
126 {
127 #ifdef CONFIG_GDBSTUB
128         gdbstub_exit(0);
129 #endif
130
131         for (;;);
132 }
133
134 void machine_power_off(void)
135 {
136 #ifdef CONFIG_GDBSTUB
137         gdbstub_exit(0);
138 #endif
139
140         for (;;);
141 }
142
143 void flush_thread(void)
144 {
145 #if 0 //ndef NO_FPU
146         unsigned long zero = 0;
147 #endif
148         set_fs(USER_DS);
149 }
150
151 inline unsigned long user_stack(const struct pt_regs *regs)
152 {
153         while (regs->next_frame)
154                 regs = regs->next_frame;
155         return user_mode(regs) ? regs->sp : 0;
156 }
157
158 asmlinkage int sys_fork(void)
159 {
160 #ifndef CONFIG_MMU
161         /* fork almost works, enough to trick you into looking elsewhere:-( */
162         return -EINVAL;
163 #else
164         return do_fork(SIGCHLD, user_stack(__frame), __frame, 0, NULL, NULL);
165 #endif
166 }
167
168 asmlinkage int sys_vfork(void)
169 {
170         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, user_stack(__frame), __frame, 0,
171                        NULL, NULL);
172 }
173
174 /*****************************************************************************/
175 /*
176  * clone a process
177  * - tlsptr is retrieved by copy_thread()
178  */
179 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
180                          int __user *parent_tidptr, int __user *child_tidptr,
181                          int __user *tlsptr)
182 {
183         if (!newsp)
184                 newsp = user_stack(__frame);
185         return do_fork(clone_flags, newsp, __frame, 0, parent_tidptr, child_tidptr);
186 } /* end sys_clone() */
187
188 /*****************************************************************************/
189 /*
190  * This gets called before we allocate a new thread and copy
191  * the current task into it.
192  */
193 void prepare_to_copy(struct task_struct *tsk)
194 {
195         //unlazy_fpu(tsk);
196 } /* end prepare_to_copy() */
197
198 /*****************************************************************************/
199 /*
200  * set up the kernel stack and exception frames for a new process
201  */
202 int copy_thread(unsigned long clone_flags,
203                 unsigned long usp, unsigned long topstk,
204                 struct task_struct *p, struct pt_regs *regs)
205 {
206         struct pt_regs *childregs0, *childregs, *regs0;
207
208         regs0 = __kernel_frame0_ptr;
209         childregs0 = (struct pt_regs *)
210                 (task_stack_page(p) + THREAD_SIZE - FRV_FRAME0_SIZE);
211         childregs = childregs0;
212
213         /* set up the userspace frame (the only place that the USP is stored) */
214         *childregs0 = *regs0;
215
216         childregs0->gr8         = 0;
217         childregs0->sp          = usp;
218         childregs0->next_frame  = NULL;
219
220         /* set up the return kernel frame if called from kernel_thread() */
221         if (regs != regs0) {
222                 childregs--;
223                 *childregs = *regs;
224                 childregs->sp = (unsigned long) childregs0;
225                 childregs->next_frame = childregs0;
226                 childregs->gr15 = (unsigned long) task_thread_info(p);
227                 childregs->gr29 = (unsigned long) p;
228         }
229
230         p->set_child_tid = p->clear_child_tid = NULL;
231
232         p->thread.frame  = childregs;
233         p->thread.curr   = p;
234         p->thread.sp     = (unsigned long) childregs;
235         p->thread.fp     = 0;
236         p->thread.lr     = 0;
237         p->thread.pc     = (unsigned long) ret_from_fork;
238         p->thread.frame0 = childregs0;
239
240         /* the new TLS pointer is passed in as arg #5 to sys_clone() */
241         if (clone_flags & CLONE_SETTLS)
242                 childregs->gr29 = childregs->gr12;
243
244         save_user_regs(p->thread.user);
245
246         return 0;
247 } /* end copy_thread() */
248
249 /*
250  * sys_execve() executes a new program.
251  */
252 asmlinkage int sys_execve(const char __user *name,
253                           const char __user *const __user *argv,
254                           const char __user *const __user *envp)
255 {
256         int error;
257         char * filename;
258
259         filename = getname(name);
260         error = PTR_ERR(filename);
261         if (IS_ERR(filename))
262                 return error;
263         error = do_execve(filename, argv, envp, __frame);
264         putname(filename);
265         return error;
266 }
267
268 unsigned long get_wchan(struct task_struct *p)
269 {
270         struct pt_regs *regs0;
271         unsigned long fp, pc;
272         unsigned long stack_limit;
273         int count = 0;
274         if (!p || p == current || p->state == TASK_RUNNING)
275                 return 0;
276
277         stack_limit = (unsigned long) (p + 1);
278         fp = p->thread.fp;
279         regs0 = p->thread.frame0;
280
281         do {
282                 if (fp < stack_limit || fp >= (unsigned long) regs0 || fp & 3)
283                         return 0;
284
285                 pc = ((unsigned long *) fp)[2];
286
287                 /* FIXME: This depends on the order of these functions. */
288                 if (!in_sched_functions(pc))
289                         return pc;
290
291                 fp = *(unsigned long *) fp;
292         } while (count++ < 16);
293
294         return 0;
295 }
296
297 unsigned long thread_saved_pc(struct task_struct *tsk)
298 {
299         /* Check whether the thread is blocked in resume() */
300         if (in_sched_functions(tsk->thread.pc))
301                 return ((unsigned long *)tsk->thread.fp)[2];
302         else
303                 return tsk->thread.pc;
304 }
305
306 int elf_check_arch(const struct elf32_hdr *hdr)
307 {
308         unsigned long hsr0 = __get_HSR(0);
309         unsigned long psr = __get_PSR();
310
311         if (hdr->e_machine != EM_FRV)
312                 return 0;
313
314         switch (hdr->e_flags & EF_FRV_GPR_MASK) {
315         case EF_FRV_GPR64:
316                 if ((hsr0 & HSR0_GRN) == HSR0_GRN_32)
317                         return 0;
318         case EF_FRV_GPR32:
319         case 0:
320                 break;
321         default:
322                 return 0;
323         }
324
325         switch (hdr->e_flags & EF_FRV_FPR_MASK) {
326         case EF_FRV_FPR64:
327                 if ((hsr0 & HSR0_FRN) == HSR0_FRN_32)
328                         return 0;
329         case EF_FRV_FPR32:
330         case EF_FRV_FPR_NONE:
331         case 0:
332                 break;
333         default:
334                 return 0;
335         }
336
337         if ((hdr->e_flags & EF_FRV_MULADD) == EF_FRV_MULADD)
338                 if (PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
339                     PSR_IMPLE(psr) != PSR_IMPLE_FR451)
340                         return 0;
341
342         switch (hdr->e_flags & EF_FRV_CPU_MASK) {
343         case EF_FRV_CPU_GENERIC:
344                 break;
345         case EF_FRV_CPU_FR300:
346         case EF_FRV_CPU_SIMPLE:
347         case EF_FRV_CPU_TOMCAT:
348         default:
349                 return 0;
350         case EF_FRV_CPU_FR400:
351                 if (PSR_IMPLE(psr) != PSR_IMPLE_FR401 &&
352                     PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
353                     PSR_IMPLE(psr) != PSR_IMPLE_FR451 &&
354                     PSR_IMPLE(psr) != PSR_IMPLE_FR551)
355                         return 0;
356                 break;
357         case EF_FRV_CPU_FR450:
358                 if (PSR_IMPLE(psr) != PSR_IMPLE_FR451)
359                         return 0;
360                 break;
361         case EF_FRV_CPU_FR500:
362                 if (PSR_IMPLE(psr) != PSR_IMPLE_FR501)
363                         return 0;
364                 break;
365         case EF_FRV_CPU_FR550:
366                 if (PSR_IMPLE(psr) != PSR_IMPLE_FR551)
367                         return 0;
368                 break;
369         }
370
371         return 1;
372 }
373
374 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
375 {
376         memcpy(fpregs,
377                &current->thread.user->f,
378                sizeof(current->thread.user->f));
379         return 1;
380 }