Pull bugzilla-7880 into release branch
[linux-2.6] / arch / blackfin / kernel / process.c
1 /*
2  * File:         arch/blackfin/kernel/process.c
3  * Based on:
4  * Author:
5  *
6  * Created:
7  * Description:  Blackfin architecture-dependent process handling.
8  *
9  * Modified:
10  *               Copyright 2004-2006 Analog Devices Inc.
11  *
12  * Bugs:         Enter bugs at http://blackfin.uclinux.org/
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, see the file COPYING, or write
26  * to the Free Software Foundation, Inc.,
27  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28  */
29
30 #include <linux/module.h>
31 #include <linux/smp_lock.h>
32 #include <linux/unistd.h>
33 #include <linux/user.h>
34 #include <linux/a.out.h>
35 #include <linux/uaccess.h>
36
37 #include <asm/blackfin.h>
38 #include <asm/fixed_code.h>
39
40 #define LED_ON  0
41 #define LED_OFF 1
42
43 asmlinkage void ret_from_fork(void);
44
45 /* Points to the SDRAM backup memory for the stack that is currently in
46  * L1 scratchpad memory.
47  */
48 void *current_l1_stack_save;
49
50 /* The number of tasks currently using a L1 stack area.  The SRAM is
51  * allocated/deallocated whenever this changes from/to zero.
52  */
53 int nr_l1stack_tasks;
54
55 /* Start and length of the area in L1 scratchpad memory which we've allocated
56  * for process stacks.
57  */
58 void *l1_stack_base;
59 unsigned long l1_stack_len;
60
61 /*
62  * Powermanagement idle function, if any..
63  */
64 void (*pm_idle)(void) = NULL;
65 EXPORT_SYMBOL(pm_idle);
66
67 void (*pm_power_off)(void) = NULL;
68 EXPORT_SYMBOL(pm_power_off);
69
70 /*
71  * We are using a different LED from the one used to indicate timer interrupt.
72  */
73 #if defined(CONFIG_BFIN_IDLE_LED)
74 static inline void leds_switch(int flag)
75 {
76         unsigned short tmp = 0;
77
78         tmp = bfin_read_CONFIG_BFIN_IDLE_LED_PORT();
79         SSYNC();
80
81         if (flag == LED_ON)
82                 tmp &= ~CONFIG_BFIN_IDLE_LED_PIN;       /* light on */
83         else
84                 tmp |= CONFIG_BFIN_IDLE_LED_PIN;        /* light off */
85
86         bfin_write_CONFIG_BFIN_IDLE_LED_PORT(tmp);
87         SSYNC();
88
89 }
90 #else
91 static inline void leds_switch(int flag)
92 {
93 }
94 #endif
95
96 /*
97  * The idle loop on BFIN
98  */
99 #ifdef CONFIG_IDLE_L1
100 void default_idle(void)__attribute__((l1_text));
101 void cpu_idle(void)__attribute__((l1_text));
102 #endif
103
104 void default_idle(void)
105 {
106         while (!need_resched()) {
107                 leds_switch(LED_OFF);
108                 local_irq_disable();
109                 if (likely(!need_resched()))
110                         idle_with_irq_disabled();
111                 local_irq_enable();
112                 leds_switch(LED_ON);
113         }
114 }
115
116 void (*idle)(void) = default_idle;
117
118 /*
119  * The idle thread. There's no useful work to be
120  * done, so just try to conserve power and have a
121  * low exit latency (ie sit in a loop waiting for
122  * somebody to say that they'd like to reschedule)
123  */
124 void cpu_idle(void)
125 {
126         /* endless idle loop with no priority at all */
127         while (1) {
128                 idle();
129                 preempt_enable_no_resched();
130                 schedule();
131                 preempt_disable();
132         }
133 }
134
135 void machine_restart(char *__unused)
136 {
137 #if defined(CONFIG_BLKFIN_CACHE)
138         bfin_write_IMEM_CONTROL(0x01);
139         SSYNC();
140 #endif
141         bfin_reset();
142         /* Dont do anything till the reset occurs */
143         while (1) {
144                 SSYNC();
145         }
146 }
147
148 void machine_halt(void)
149 {
150         for (;;)
151                 asm volatile ("idle");
152 }
153
154 void machine_power_off(void)
155 {
156         for (;;)
157                 asm volatile ("idle");
158 }
159
160 void show_regs(struct pt_regs *regs)
161 {
162         printk(KERN_NOTICE "\n");
163         printk(KERN_NOTICE
164                "PC: %08lu  Status: %04lu  SysStatus: %04lu  RETS: %08lu\n",
165                regs->pc, regs->astat, regs->seqstat, regs->rets);
166         printk(KERN_NOTICE
167                "A0.x: %08lx  A0.w: %08lx  A1.x: %08lx  A1.w: %08lx\n",
168                regs->a0x, regs->a0w, regs->a1x, regs->a1w);
169         printk(KERN_NOTICE "P0: %08lx  P1: %08lx  P2: %08lx  P3: %08lx\n",
170                regs->p0, regs->p1, regs->p2, regs->p3);
171         printk(KERN_NOTICE "P4: %08lx  P5: %08lx\n", regs->p4, regs->p5);
172         printk(KERN_NOTICE "R0: %08lx  R1: %08lx  R2: %08lx  R3: %08lx\n",
173                regs->r0, regs->r1, regs->r2, regs->r3);
174         printk(KERN_NOTICE "R4: %08lx  R5: %08lx  R6: %08lx  R7: %08lx\n",
175                regs->r4, regs->r5, regs->r6, regs->r7);
176
177         if (!regs->ipend)
178                 printk(KERN_NOTICE "USP: %08lx\n", rdusp());
179 }
180
181 /* Fill in the fpu structure for a core dump.  */
182
183 int dump_fpu(struct pt_regs *regs, elf_fpregset_t * fpregs)
184 {
185         return 1;
186 }
187
188 /*
189  * This gets run with P1 containing the
190  * function to call, and R1 containing
191  * the "args".  Note P0 is clobbered on the way here.
192  */
193 void kernel_thread_helper(void);
194 __asm__(".section .text\n"
195         ".align 4\n"
196         "_kernel_thread_helper:\n\t"
197         "\tsp += -12;\n\t"
198         "\tr0 = r1;\n\t" "\tcall (p1);\n\t" "\tcall _do_exit;\n" ".previous");
199
200 /*
201  * Create a kernel thread.
202  */
203 pid_t kernel_thread(int (*fn) (void *), void *arg, unsigned long flags)
204 {
205         struct pt_regs regs;
206
207         memset(&regs, 0, sizeof(regs));
208
209         regs.r1 = (unsigned long)arg;
210         regs.p1 = (unsigned long)fn;
211         regs.pc = (unsigned long)kernel_thread_helper;
212         regs.orig_p0 = -1;
213         /* Set bit 2 to tell ret_from_fork we should be returning to kernel
214            mode.  */
215         regs.ipend = 0x8002;
216         __asm__ __volatile__("%0 = syscfg;":"=da"(regs.syscfg):);
217         return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL,
218                        NULL);
219 }
220
221 void flush_thread(void)
222 {
223 }
224
225 asmlinkage int bfin_vfork(struct pt_regs *regs)
226 {
227         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL,
228                        NULL);
229 }
230
231 asmlinkage int bfin_clone(struct pt_regs *regs)
232 {
233         unsigned long clone_flags;
234         unsigned long newsp;
235
236         /* syscall2 puts clone_flags in r0 and usp in r1 */
237         clone_flags = regs->r0;
238         newsp = regs->r1;
239         if (!newsp)
240                 newsp = rdusp();
241         else
242                 newsp -= 12;
243         return do_fork(clone_flags, newsp, regs, 0, NULL, NULL);
244 }
245
246 int
247 copy_thread(int nr, unsigned long clone_flags,
248             unsigned long usp, unsigned long topstk,
249             struct task_struct *p, struct pt_regs *regs)
250 {
251         struct pt_regs *childregs;
252
253         childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
254         *childregs = *regs;
255         childregs->r0 = 0;
256
257         p->thread.usp = usp;
258         p->thread.ksp = (unsigned long)childregs;
259         p->thread.pc = (unsigned long)ret_from_fork;
260
261         return 0;
262 }
263
264 /*
265  * fill in the user structure for a core dump..
266  */
267 void dump_thread(struct pt_regs *regs, struct user *dump)
268 {
269         dump->magic = CMAGIC;
270         dump->start_code = 0;
271         dump->start_stack = rdusp() & ~(PAGE_SIZE - 1);
272         dump->u_tsize = ((unsigned long)current->mm->end_code) >> PAGE_SHIFT;
273         dump->u_dsize = ((unsigned long)(current->mm->brk +
274                                          (PAGE_SIZE - 1))) >> PAGE_SHIFT;
275         dump->u_dsize -= dump->u_tsize;
276         dump->u_ssize = 0;
277
278         if (dump->start_stack < TASK_SIZE)
279                 dump->u_ssize =
280                     ((unsigned long)(TASK_SIZE -
281                                      dump->start_stack)) >> PAGE_SHIFT;
282
283         dump->u_ar0 = (struct user_regs_struct *)((int)&dump->regs - (int)dump);
284
285         dump->regs.r0 = regs->r0;
286         dump->regs.r1 = regs->r1;
287         dump->regs.r2 = regs->r2;
288         dump->regs.r3 = regs->r3;
289         dump->regs.r4 = regs->r4;
290         dump->regs.r5 = regs->r5;
291         dump->regs.r6 = regs->r6;
292         dump->regs.r7 = regs->r7;
293         dump->regs.p0 = regs->p0;
294         dump->regs.p1 = regs->p1;
295         dump->regs.p2 = regs->p2;
296         dump->regs.p3 = regs->p3;
297         dump->regs.p4 = regs->p4;
298         dump->regs.p5 = regs->p5;
299         dump->regs.orig_p0 = regs->orig_p0;
300         dump->regs.a0w = regs->a0w;
301         dump->regs.a1w = regs->a1w;
302         dump->regs.a0x = regs->a0x;
303         dump->regs.a1x = regs->a1x;
304         dump->regs.rets = regs->rets;
305         dump->regs.astat = regs->astat;
306         dump->regs.pc = regs->pc;
307 }
308
309 /*
310  * sys_execve() executes a new program.
311  */
312
313 asmlinkage int sys_execve(char *name, char **argv, char **envp)
314 {
315         int error;
316         char *filename;
317         struct pt_regs *regs = (struct pt_regs *)((&name) + 6);
318
319         lock_kernel();
320         filename = getname(name);
321         error = PTR_ERR(filename);
322         if (IS_ERR(filename))
323                 goto out;
324         error = do_execve(filename, argv, envp, regs);
325         putname(filename);
326  out:
327         unlock_kernel();
328         return error;
329 }
330
331 unsigned long get_wchan(struct task_struct *p)
332 {
333         unsigned long fp, pc;
334         unsigned long stack_page;
335         int count = 0;
336         if (!p || p == current || p->state == TASK_RUNNING)
337                 return 0;
338
339         stack_page = (unsigned long)p;
340         fp = p->thread.usp;
341         do {
342                 if (fp < stack_page + sizeof(struct thread_info) ||
343                     fp >= 8184 + stack_page)
344                         return 0;
345                 pc = ((unsigned long *)fp)[1];
346                 if (!in_sched_functions(pc))
347                         return pc;
348                 fp = *(unsigned long *)fp;
349         }
350         while (count++ < 16);
351         return 0;
352 }
353
354 void finish_atomic_sections (struct pt_regs *regs)
355 {
356         if (regs->pc < ATOMIC_SEQS_START || regs->pc >= ATOMIC_SEQS_END)
357                 return;
358
359         switch (regs->pc) {
360         case ATOMIC_XCHG32 + 2:
361                 put_user(regs->r1, (int *)regs->p0);
362                 regs->pc += 2;
363                 break;
364
365         case ATOMIC_CAS32 + 2:
366         case ATOMIC_CAS32 + 4:
367                 if (regs->r0 == regs->r1)
368                         put_user(regs->r2, (int *)regs->p0);
369                 regs->pc = ATOMIC_CAS32 + 8;
370                 break;
371         case ATOMIC_CAS32 + 6:
372                 put_user(regs->r2, (int *)regs->p0);
373                 regs->pc += 2;
374                 break;
375
376         case ATOMIC_ADD32 + 2:
377                 regs->r0 = regs->r1 + regs->r0;
378                 /* fall through */
379         case ATOMIC_ADD32 + 4:
380                 put_user(regs->r0, (int *)regs->p0);
381                 regs->pc = ATOMIC_ADD32 + 6;
382                 break;
383
384         case ATOMIC_SUB32 + 2:
385                 regs->r0 = regs->r1 - regs->r0;
386                 /* fall through */
387         case ATOMIC_SUB32 + 4:
388                 put_user(regs->r0, (int *)regs->p0);
389                 regs->pc = ATOMIC_SUB32 + 6;
390                 break;
391
392         case ATOMIC_IOR32 + 2:
393                 regs->r0 = regs->r1 | regs->r0;
394                 /* fall through */
395         case ATOMIC_IOR32 + 4:
396                 put_user(regs->r0, (int *)regs->p0);
397                 regs->pc = ATOMIC_IOR32 + 6;
398                 break;
399
400         case ATOMIC_AND32 + 2:
401                 regs->r0 = regs->r1 & regs->r0;
402                 /* fall through */
403         case ATOMIC_AND32 + 4:
404                 put_user(regs->r0, (int *)regs->p0);
405                 regs->pc = ATOMIC_AND32 + 6;
406                 break;
407
408         case ATOMIC_XOR32 + 2:
409                 regs->r0 = regs->r1 ^ regs->r0;
410                 /* fall through */
411         case ATOMIC_XOR32 + 4:
412                 put_user(regs->r0, (int *)regs->p0);
413                 regs->pc = ATOMIC_XOR32 + 6;
414                 break;
415         }
416 }
417
418 #if defined(CONFIG_ACCESS_CHECK)
419 int _access_ok(unsigned long addr, unsigned long size)
420 {
421
422         if (addr > (addr + size))
423                 return 0;
424         if (segment_eq(get_fs(), KERNEL_DS))
425                 return 1;
426 #ifdef CONFIG_MTD_UCLINUX
427         if (addr >= memory_start && (addr + size) <= memory_end)
428                 return 1;
429         if (addr >= memory_mtd_end && (addr + size) <= physical_mem_end)
430                 return 1;
431 #else
432         if (addr >= memory_start && (addr + size) <= physical_mem_end)
433                 return 1;
434 #endif
435         if (addr >= (unsigned long)__init_begin &&
436             addr + size <= (unsigned long)__init_end)
437                 return 1;
438         if (addr >= L1_SCRATCH_START
439             && addr + size <= L1_SCRATCH_START + L1_SCRATCH_LENGTH)
440                 return 1;
441 #if L1_CODE_LENGTH != 0
442         if (addr >= L1_CODE_START + (_etext_l1 - _stext_l1)
443             && addr + size <= L1_CODE_START + L1_CODE_LENGTH)
444                 return 1;
445 #endif
446 #if L1_DATA_A_LENGTH != 0
447         if (addr >= L1_DATA_A_START + (_ebss_l1 - _sdata_l1)
448             && addr + size <= L1_DATA_A_START + L1_DATA_A_LENGTH)
449                 return 1;
450 #endif
451 #if L1_DATA_B_LENGTH != 0
452         if (addr >= L1_DATA_B_START
453             && addr + size <= L1_DATA_B_START + L1_DATA_B_LENGTH)
454                 return 1;
455 #endif
456         return 0;
457 }
458 EXPORT_SYMBOL(_access_ok);
459 #endif /* CONFIG_ACCESS_CHECK */