]> Pileus Git - ~andy/linux/blob - arch/frv/kernel/process.c
Merge branch 'omap/dt-missed-3.4' into drivers/mmc
[~andy/linux] / 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/setup.h>
32 #include <asm/pgtable.h>
33 #include <asm/tlb.h>
34 #include <asm/gdb-stub.h>
35 #include <asm/mb-regs.h>
36
37 #include "local.h"
38
39 asmlinkage void ret_from_fork(void);
40
41 #include <asm/pgalloc.h>
42
43 void (*pm_power_off)(void);
44 EXPORT_SYMBOL(pm_power_off);
45
46 struct task_struct *alloc_task_struct_node(int node)
47 {
48         struct task_struct *p = kmalloc_node(THREAD_SIZE, GFP_KERNEL, node);
49
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                 schedule_preempt_disabled();
95         }
96 }
97
98 void machine_restart(char * __unused)
99 {
100         unsigned long reset_addr;
101 #ifdef CONFIG_GDBSTUB
102         gdbstub_exit(0);
103 #endif
104
105         if (PSR_IMPLE(__get_PSR()) == PSR_IMPLE_FR551)
106                 reset_addr = 0xfefff500;
107         else
108                 reset_addr = 0xfeff0500;
109
110         /* Software reset. */
111         asm volatile("      dcef @(gr0,gr0),1 ! membar !"
112                      "      sti     %1,@(%0,0) !"
113                      "      nop ! nop ! nop ! nop ! nop ! "
114                      "      nop ! nop ! nop ! nop ! nop ! "
115                      "      nop ! nop ! nop ! nop ! nop ! "
116                      "      nop ! nop ! nop ! nop ! nop ! "
117                      : : "r" (reset_addr), "r" (1) );
118
119         for (;;)
120                 ;
121 }
122
123 void machine_halt(void)
124 {
125 #ifdef CONFIG_GDBSTUB
126         gdbstub_exit(0);
127 #endif
128
129         for (;;);
130 }
131
132 void machine_power_off(void)
133 {
134 #ifdef CONFIG_GDBSTUB
135         gdbstub_exit(0);
136 #endif
137
138         for (;;);
139 }
140
141 void flush_thread(void)
142 {
143         /* nothing */
144 }
145
146 inline unsigned long user_stack(const struct pt_regs *regs)
147 {
148         while (regs->next_frame)
149                 regs = regs->next_frame;
150         return user_mode(regs) ? regs->sp : 0;
151 }
152
153 asmlinkage int sys_fork(void)
154 {
155 #ifndef CONFIG_MMU
156         /* fork almost works, enough to trick you into looking elsewhere:-( */
157         return -EINVAL;
158 #else
159         return do_fork(SIGCHLD, user_stack(__frame), __frame, 0, NULL, NULL);
160 #endif
161 }
162
163 asmlinkage int sys_vfork(void)
164 {
165         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, user_stack(__frame), __frame, 0,
166                        NULL, NULL);
167 }
168
169 /*****************************************************************************/
170 /*
171  * clone a process
172  * - tlsptr is retrieved by copy_thread()
173  */
174 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
175                          int __user *parent_tidptr, int __user *child_tidptr,
176                          int __user *tlsptr)
177 {
178         if (!newsp)
179                 newsp = user_stack(__frame);
180         return do_fork(clone_flags, newsp, __frame, 0, parent_tidptr, child_tidptr);
181 } /* end sys_clone() */
182
183 /*****************************************************************************/
184 /*
185  * This gets called before we allocate a new thread and copy
186  * the current task into it.
187  */
188 void prepare_to_copy(struct task_struct *tsk)
189 {
190         //unlazy_fpu(tsk);
191 } /* end prepare_to_copy() */
192
193 /*****************************************************************************/
194 /*
195  * set up the kernel stack and exception frames for a new process
196  */
197 int copy_thread(unsigned long clone_flags,
198                 unsigned long usp, unsigned long topstk,
199                 struct task_struct *p, struct pt_regs *regs)
200 {
201         struct pt_regs *childregs0, *childregs, *regs0;
202
203         regs0 = __kernel_frame0_ptr;
204         childregs0 = (struct pt_regs *)
205                 (task_stack_page(p) + THREAD_SIZE - FRV_FRAME0_SIZE);
206         childregs = childregs0;
207
208         /* set up the userspace frame (the only place that the USP is stored) */
209         *childregs0 = *regs0;
210
211         childregs0->gr8         = 0;
212         childregs0->sp          = usp;
213         childregs0->next_frame  = NULL;
214
215         /* set up the return kernel frame if called from kernel_thread() */
216         if (regs != regs0) {
217                 childregs--;
218                 *childregs = *regs;
219                 childregs->sp = (unsigned long) childregs0;
220                 childregs->next_frame = childregs0;
221                 childregs->gr15 = (unsigned long) task_thread_info(p);
222                 childregs->gr29 = (unsigned long) p;
223         }
224
225         p->set_child_tid = p->clear_child_tid = NULL;
226
227         p->thread.frame  = childregs;
228         p->thread.curr   = p;
229         p->thread.sp     = (unsigned long) childregs;
230         p->thread.fp     = 0;
231         p->thread.lr     = 0;
232         p->thread.pc     = (unsigned long) ret_from_fork;
233         p->thread.frame0 = childregs0;
234
235         /* the new TLS pointer is passed in as arg #5 to sys_clone() */
236         if (clone_flags & CLONE_SETTLS)
237                 childregs->gr29 = childregs->gr12;
238
239         save_user_regs(p->thread.user);
240
241         return 0;
242 } /* end copy_thread() */
243
244 /*
245  * sys_execve() executes a new program.
246  */
247 asmlinkage int sys_execve(const char __user *name,
248                           const char __user *const __user *argv,
249                           const char __user *const __user *envp)
250 {
251         int error;
252         char * filename;
253
254         filename = getname(name);
255         error = PTR_ERR(filename);
256         if (IS_ERR(filename))
257                 return error;
258         error = do_execve(filename, argv, envp, __frame);
259         putname(filename);
260         return error;
261 }
262
263 unsigned long get_wchan(struct task_struct *p)
264 {
265         struct pt_regs *regs0;
266         unsigned long fp, pc;
267         unsigned long stack_limit;
268         int count = 0;
269         if (!p || p == current || p->state == TASK_RUNNING)
270                 return 0;
271
272         stack_limit = (unsigned long) (p + 1);
273         fp = p->thread.fp;
274         regs0 = p->thread.frame0;
275
276         do {
277                 if (fp < stack_limit || fp >= (unsigned long) regs0 || fp & 3)
278                         return 0;
279
280                 pc = ((unsigned long *) fp)[2];
281
282                 /* FIXME: This depends on the order of these functions. */
283                 if (!in_sched_functions(pc))
284                         return pc;
285
286                 fp = *(unsigned long *) fp;
287         } while (count++ < 16);
288
289         return 0;
290 }
291
292 unsigned long thread_saved_pc(struct task_struct *tsk)
293 {
294         /* Check whether the thread is blocked in resume() */
295         if (in_sched_functions(tsk->thread.pc))
296                 return ((unsigned long *)tsk->thread.fp)[2];
297         else
298                 return tsk->thread.pc;
299 }
300
301 int elf_check_arch(const struct elf32_hdr *hdr)
302 {
303         unsigned long hsr0 = __get_HSR(0);
304         unsigned long psr = __get_PSR();
305
306         if (hdr->e_machine != EM_FRV)
307                 return 0;
308
309         switch (hdr->e_flags & EF_FRV_GPR_MASK) {
310         case EF_FRV_GPR64:
311                 if ((hsr0 & HSR0_GRN) == HSR0_GRN_32)
312                         return 0;
313         case EF_FRV_GPR32:
314         case 0:
315                 break;
316         default:
317                 return 0;
318         }
319
320         switch (hdr->e_flags & EF_FRV_FPR_MASK) {
321         case EF_FRV_FPR64:
322                 if ((hsr0 & HSR0_FRN) == HSR0_FRN_32)
323                         return 0;
324         case EF_FRV_FPR32:
325         case EF_FRV_FPR_NONE:
326         case 0:
327                 break;
328         default:
329                 return 0;
330         }
331
332         if ((hdr->e_flags & EF_FRV_MULADD) == EF_FRV_MULADD)
333                 if (PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
334                     PSR_IMPLE(psr) != PSR_IMPLE_FR451)
335                         return 0;
336
337         switch (hdr->e_flags & EF_FRV_CPU_MASK) {
338         case EF_FRV_CPU_GENERIC:
339                 break;
340         case EF_FRV_CPU_FR300:
341         case EF_FRV_CPU_SIMPLE:
342         case EF_FRV_CPU_TOMCAT:
343         default:
344                 return 0;
345         case EF_FRV_CPU_FR400:
346                 if (PSR_IMPLE(psr) != PSR_IMPLE_FR401 &&
347                     PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
348                     PSR_IMPLE(psr) != PSR_IMPLE_FR451 &&
349                     PSR_IMPLE(psr) != PSR_IMPLE_FR551)
350                         return 0;
351                 break;
352         case EF_FRV_CPU_FR450:
353                 if (PSR_IMPLE(psr) != PSR_IMPLE_FR451)
354                         return 0;
355                 break;
356         case EF_FRV_CPU_FR500:
357                 if (PSR_IMPLE(psr) != PSR_IMPLE_FR501)
358                         return 0;
359                 break;
360         case EF_FRV_CPU_FR550:
361                 if (PSR_IMPLE(psr) != PSR_IMPLE_FR551)
362                         return 0;
363                 break;
364         }
365
366         return 1;
367 }
368
369 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
370 {
371         memcpy(fpregs,
372                &current->thread.user->f,
373                sizeof(current->thread.user->f));
374         return 1;
375 }