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