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