2 * File: arch/blackfin/kernel/process.c
7 * Description: Blackfin architecture-dependent process handling.
10 * Copyright 2004-2006 Analog Devices Inc.
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
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.
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.
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
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>
37 #include <linux/err.h>
39 #include <asm/blackfin.h>
40 #include <asm/fixed_code.h>
45 asmlinkage void ret_from_fork(void);
47 /* Points to the SDRAM backup memory for the stack that is currently in
48 * L1 scratchpad memory.
50 void *current_l1_stack_save;
52 /* The number of tasks currently using a L1 stack area. The SRAM is
53 * allocated/deallocated whenever this changes from/to zero.
57 /* Start and length of the area in L1 scratchpad memory which we've allocated
61 unsigned long l1_stack_len;
64 * Powermanagement idle function, if any..
66 void (*pm_idle)(void) = NULL;
67 EXPORT_SYMBOL(pm_idle);
69 void (*pm_power_off)(void) = NULL;
70 EXPORT_SYMBOL(pm_power_off);
73 * We are using a different LED from the one used to indicate timer interrupt.
75 #if defined(CONFIG_BFIN_IDLE_LED)
76 static inline void leds_switch(int flag)
78 unsigned short tmp = 0;
80 tmp = bfin_read_CONFIG_BFIN_IDLE_LED_PORT();
84 tmp &= ~CONFIG_BFIN_IDLE_LED_PIN; /* light on */
86 tmp |= CONFIG_BFIN_IDLE_LED_PIN; /* light off */
88 bfin_write_CONFIG_BFIN_IDLE_LED_PORT(tmp);
93 static inline void leds_switch(int flag)
99 * The idle loop on BFIN
101 #ifdef CONFIG_IDLE_L1
102 void default_idle(void)__attribute__((l1_text));
103 void cpu_idle(void)__attribute__((l1_text));
106 void default_idle(void)
108 while (!need_resched()) {
109 leds_switch(LED_OFF);
111 if (likely(!need_resched()))
112 idle_with_irq_disabled();
118 void (*idle)(void) = default_idle;
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)
128 /* endless idle loop with no priority at all */
131 preempt_enable_no_resched();
137 void show_regs(struct pt_regs *regs)
139 printk(KERN_NOTICE "\n");
141 "PC: %08lu Status: %04lu SysStatus: %04lu RETS: %08lu\n",
142 regs->pc, regs->astat, regs->seqstat, regs->rets);
144 "A0.x: %08lx A0.w: %08lx A1.x: %08lx A1.w: %08lx\n",
145 regs->a0x, regs->a0w, regs->a1x, regs->a1w);
146 printk(KERN_NOTICE "P0: %08lx P1: %08lx P2: %08lx P3: %08lx\n",
147 regs->p0, regs->p1, regs->p2, regs->p3);
148 printk(KERN_NOTICE "P4: %08lx P5: %08lx\n", regs->p4, regs->p5);
149 printk(KERN_NOTICE "R0: %08lx R1: %08lx R2: %08lx R3: %08lx\n",
150 regs->r0, regs->r1, regs->r2, regs->r3);
151 printk(KERN_NOTICE "R4: %08lx R5: %08lx R6: %08lx R7: %08lx\n",
152 regs->r4, regs->r5, regs->r6, regs->r7);
155 printk(KERN_NOTICE "USP: %08lx\n", rdusp());
158 /* Fill in the fpu structure for a core dump. */
160 int dump_fpu(struct pt_regs *regs, elf_fpregset_t * fpregs)
166 * This gets run with P1 containing the
167 * function to call, and R1 containing
168 * the "args". Note P0 is clobbered on the way here.
170 void kernel_thread_helper(void);
171 __asm__(".section .text\n"
173 "_kernel_thread_helper:\n\t"
175 "\tr0 = r1;\n\t" "\tcall (p1);\n\t" "\tcall _do_exit;\n" ".previous");
178 * Create a kernel thread.
180 pid_t kernel_thread(int (*fn) (void *), void *arg, unsigned long flags)
184 memset(®s, 0, sizeof(regs));
186 regs.r1 = (unsigned long)arg;
187 regs.p1 = (unsigned long)fn;
188 regs.pc = (unsigned long)kernel_thread_helper;
190 /* Set bit 2 to tell ret_from_fork we should be returning to kernel
193 __asm__ __volatile__("%0 = syscfg;":"=da"(regs.syscfg):);
194 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, ®s, 0, NULL,
198 void flush_thread(void)
202 asmlinkage int bfin_vfork(struct pt_regs *regs)
204 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL,
208 asmlinkage int bfin_clone(struct pt_regs *regs)
210 unsigned long clone_flags;
213 /* syscall2 puts clone_flags in r0 and usp in r1 */
214 clone_flags = regs->r0;
220 return do_fork(clone_flags, newsp, regs, 0, NULL, NULL);
224 copy_thread(int nr, unsigned long clone_flags,
225 unsigned long usp, unsigned long topstk,
226 struct task_struct *p, struct pt_regs *regs)
228 struct pt_regs *childregs;
230 childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
235 p->thread.ksp = (unsigned long)childregs;
236 p->thread.pc = (unsigned long)ret_from_fork;
242 * fill in the user structure for a core dump..
244 void dump_thread(struct pt_regs *regs, struct user *dump)
246 dump->magic = CMAGIC;
247 dump->start_code = 0;
248 dump->start_stack = rdusp() & ~(PAGE_SIZE - 1);
249 dump->u_tsize = ((unsigned long)current->mm->end_code) >> PAGE_SHIFT;
250 dump->u_dsize = ((unsigned long)(current->mm->brk +
251 (PAGE_SIZE - 1))) >> PAGE_SHIFT;
252 dump->u_dsize -= dump->u_tsize;
255 if (dump->start_stack < TASK_SIZE)
257 ((unsigned long)(TASK_SIZE -
258 dump->start_stack)) >> PAGE_SHIFT;
260 dump->u_ar0 = (struct user_regs_struct *)((int)&dump->regs - (int)dump);
262 dump->regs.r0 = regs->r0;
263 dump->regs.r1 = regs->r1;
264 dump->regs.r2 = regs->r2;
265 dump->regs.r3 = regs->r3;
266 dump->regs.r4 = regs->r4;
267 dump->regs.r5 = regs->r5;
268 dump->regs.r6 = regs->r6;
269 dump->regs.r7 = regs->r7;
270 dump->regs.p0 = regs->p0;
271 dump->regs.p1 = regs->p1;
272 dump->regs.p2 = regs->p2;
273 dump->regs.p3 = regs->p3;
274 dump->regs.p4 = regs->p4;
275 dump->regs.p5 = regs->p5;
276 dump->regs.orig_p0 = regs->orig_p0;
277 dump->regs.a0w = regs->a0w;
278 dump->regs.a1w = regs->a1w;
279 dump->regs.a0x = regs->a0x;
280 dump->regs.a1x = regs->a1x;
281 dump->regs.rets = regs->rets;
282 dump->regs.astat = regs->astat;
283 dump->regs.pc = regs->pc;
287 * sys_execve() executes a new program.
290 asmlinkage int sys_execve(char *name, char **argv, char **envp)
294 struct pt_regs *regs = (struct pt_regs *)((&name) + 6);
297 filename = getname(name);
298 error = PTR_ERR(filename);
299 if (IS_ERR(filename))
301 error = do_execve(filename, argv, envp, regs);
308 unsigned long get_wchan(struct task_struct *p)
310 unsigned long fp, pc;
311 unsigned long stack_page;
313 if (!p || p == current || p->state == TASK_RUNNING)
316 stack_page = (unsigned long)p;
319 if (fp < stack_page + sizeof(struct thread_info) ||
320 fp >= 8184 + stack_page)
322 pc = ((unsigned long *)fp)[1];
323 if (!in_sched_functions(pc))
325 fp = *(unsigned long *)fp;
327 while (count++ < 16);
331 void finish_atomic_sections (struct pt_regs *regs)
333 if (regs->pc < ATOMIC_SEQS_START || regs->pc >= ATOMIC_SEQS_END)
337 case ATOMIC_XCHG32 + 2:
338 put_user(regs->r1, (int *)regs->p0);
342 case ATOMIC_CAS32 + 2:
343 case ATOMIC_CAS32 + 4:
344 if (regs->r0 == regs->r1)
345 put_user(regs->r2, (int *)regs->p0);
346 regs->pc = ATOMIC_CAS32 + 8;
348 case ATOMIC_CAS32 + 6:
349 put_user(regs->r2, (int *)regs->p0);
353 case ATOMIC_ADD32 + 2:
354 regs->r0 = regs->r1 + regs->r0;
356 case ATOMIC_ADD32 + 4:
357 put_user(regs->r0, (int *)regs->p0);
358 regs->pc = ATOMIC_ADD32 + 6;
361 case ATOMIC_SUB32 + 2:
362 regs->r0 = regs->r1 - regs->r0;
364 case ATOMIC_SUB32 + 4:
365 put_user(regs->r0, (int *)regs->p0);
366 regs->pc = ATOMIC_SUB32 + 6;
369 case ATOMIC_IOR32 + 2:
370 regs->r0 = regs->r1 | regs->r0;
372 case ATOMIC_IOR32 + 4:
373 put_user(regs->r0, (int *)regs->p0);
374 regs->pc = ATOMIC_IOR32 + 6;
377 case ATOMIC_AND32 + 2:
378 regs->r0 = regs->r1 & regs->r0;
380 case ATOMIC_AND32 + 4:
381 put_user(regs->r0, (int *)regs->p0);
382 regs->pc = ATOMIC_AND32 + 6;
385 case ATOMIC_XOR32 + 2:
386 regs->r0 = regs->r1 ^ regs->r0;
388 case ATOMIC_XOR32 + 4:
389 put_user(regs->r0, (int *)regs->p0);
390 regs->pc = ATOMIC_XOR32 + 6;
395 #if defined(CONFIG_ACCESS_CHECK)
396 int _access_ok(unsigned long addr, unsigned long size)
400 if (addr > (addr + size))
402 if (segment_eq(get_fs(), KERNEL_DS))
404 #ifdef CONFIG_MTD_UCLINUX
405 if (addr >= memory_start && (addr + size) <= memory_end)
407 if (addr >= memory_mtd_end && (addr + size) <= physical_mem_end)
410 if (addr >= memory_start && (addr + size) <= physical_mem_end)
413 if (addr >= (unsigned long)__init_begin &&
414 addr + size <= (unsigned long)__init_end)
416 if (addr >= L1_SCRATCH_START
417 && addr + size <= L1_SCRATCH_START + L1_SCRATCH_LENGTH)
419 #if L1_CODE_LENGTH != 0
420 if (addr >= L1_CODE_START + (_etext_l1 - _stext_l1)
421 && addr + size <= L1_CODE_START + L1_CODE_LENGTH)
424 #if L1_DATA_A_LENGTH != 0
425 if (addr >= L1_DATA_A_START + (_ebss_l1 - _sdata_l1)
426 && addr + size <= L1_DATA_A_START + L1_DATA_A_LENGTH)
429 #if L1_DATA_B_LENGTH != 0
430 if (addr >= L1_DATA_B_START
431 && addr + size <= L1_DATA_B_START + L1_DATA_B_LENGTH)
436 EXPORT_SYMBOL(_access_ok);
437 #endif /* CONFIG_ACCESS_CHECK */