blob: 023dc80af1875864d8f350334fd5858e0ea78799 [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
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>
Mike Frysinger1f83b8f2007-07-12 22:58:21 +080035#include <linux/uaccess.h>
Bryan Wud31c5ab2007-08-10 13:00:42 -070036#include <linux/fs.h>
37#include <linux/err.h>
Bryan Wu1394f032007-05-06 14:50:22 -070038
39#include <asm/blackfin.h>
Bernd Schmidt7adfb582007-06-21 11:34:16 +080040#include <asm/fixed_code.h>
Bryan Wu1394f032007-05-06 14:50:22 -070041
Bryan Wu1394f032007-05-06 14:50:22 -070042asmlinkage void ret_from_fork(void);
43
44/* Points to the SDRAM backup memory for the stack that is currently in
45 * L1 scratchpad memory.
46 */
47void *current_l1_stack_save;
48
49/* The number of tasks currently using a L1 stack area. The SRAM is
50 * allocated/deallocated whenever this changes from/to zero.
51 */
52int nr_l1stack_tasks;
53
54/* Start and length of the area in L1 scratchpad memory which we've allocated
55 * for process stacks.
56 */
57void *l1_stack_base;
58unsigned long l1_stack_len;
59
60/*
61 * Powermanagement idle function, if any..
62 */
63void (*pm_idle)(void) = NULL;
64EXPORT_SYMBOL(pm_idle);
65
66void (*pm_power_off)(void) = NULL;
67EXPORT_SYMBOL(pm_power_off);
68
69/*
Bryan Wu1394f032007-05-06 14:50:22 -070070 * The idle loop on BFIN
71 */
72#ifdef CONFIG_IDLE_L1
73void default_idle(void)__attribute__((l1_text));
74void cpu_idle(void)__attribute__((l1_text));
75#endif
76
77void default_idle(void)
78{
79 while (!need_resched()) {
Bryan Wu1394f032007-05-06 14:50:22 -070080 local_irq_disable();
81 if (likely(!need_resched()))
82 idle_with_irq_disabled();
83 local_irq_enable();
Bryan Wu1394f032007-05-06 14:50:22 -070084 }
85}
86
87void (*idle)(void) = default_idle;
88
89/*
90 * The idle thread. There's no useful work to be
91 * done, so just try to conserve power and have a
92 * low exit latency (ie sit in a loop waiting for
93 * somebody to say that they'd like to reschedule)
94 */
95void cpu_idle(void)
96{
97 /* endless idle loop with no priority at all */
98 while (1) {
99 idle();
100 preempt_enable_no_resched();
101 schedule();
102 preempt_disable();
103 }
104}
105
Bryan Wu1394f032007-05-06 14:50:22 -0700106/* Fill in the fpu structure for a core dump. */
107
108int dump_fpu(struct pt_regs *regs, elf_fpregset_t * fpregs)
109{
110 return 1;
111}
112
113/*
114 * This gets run with P1 containing the
115 * function to call, and R1 containing
116 * the "args". Note P0 is clobbered on the way here.
117 */
118void kernel_thread_helper(void);
119__asm__(".section .text\n"
120 ".align 4\n"
121 "_kernel_thread_helper:\n\t"
122 "\tsp += -12;\n\t"
123 "\tr0 = r1;\n\t" "\tcall (p1);\n\t" "\tcall _do_exit;\n" ".previous");
124
125/*
126 * Create a kernel thread.
127 */
128pid_t kernel_thread(int (*fn) (void *), void *arg, unsigned long flags)
129{
130 struct pt_regs regs;
131
132 memset(&regs, 0, sizeof(regs));
133
134 regs.r1 = (unsigned long)arg;
135 regs.p1 = (unsigned long)fn;
136 regs.pc = (unsigned long)kernel_thread_helper;
137 regs.orig_p0 = -1;
138 /* Set bit 2 to tell ret_from_fork we should be returning to kernel
139 mode. */
140 regs.ipend = 0x8002;
141 __asm__ __volatile__("%0 = syscfg;":"=da"(regs.syscfg):);
142 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL,
143 NULL);
144}
145
146void flush_thread(void)
147{
148}
149
150asmlinkage int bfin_vfork(struct pt_regs *regs)
151{
152 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL,
153 NULL);
154}
155
156asmlinkage int bfin_clone(struct pt_regs *regs)
157{
158 unsigned long clone_flags;
159 unsigned long newsp;
160
161 /* syscall2 puts clone_flags in r0 and usp in r1 */
162 clone_flags = regs->r0;
163 newsp = regs->r1;
164 if (!newsp)
165 newsp = rdusp();
166 else
167 newsp -= 12;
168 return do_fork(clone_flags, newsp, regs, 0, NULL, NULL);
169}
170
171int
172copy_thread(int nr, unsigned long clone_flags,
173 unsigned long usp, unsigned long topstk,
174 struct task_struct *p, struct pt_regs *regs)
175{
176 struct pt_regs *childregs;
177
178 childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
179 *childregs = *regs;
180 childregs->r0 = 0;
181
182 p->thread.usp = usp;
183 p->thread.ksp = (unsigned long)childregs;
184 p->thread.pc = (unsigned long)ret_from_fork;
185
186 return 0;
187}
188
189/*
Bryan Wu1394f032007-05-06 14:50:22 -0700190 * sys_execve() executes a new program.
191 */
192
193asmlinkage int sys_execve(char *name, char **argv, char **envp)
194{
195 int error;
196 char *filename;
197 struct pt_regs *regs = (struct pt_regs *)((&name) + 6);
198
199 lock_kernel();
200 filename = getname(name);
201 error = PTR_ERR(filename);
202 if (IS_ERR(filename))
203 goto out;
204 error = do_execve(filename, argv, envp, regs);
205 putname(filename);
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800206 out:
Bryan Wu1394f032007-05-06 14:50:22 -0700207 unlock_kernel();
208 return error;
209}
210
211unsigned long get_wchan(struct task_struct *p)
212{
213 unsigned long fp, pc;
214 unsigned long stack_page;
215 int count = 0;
216 if (!p || p == current || p->state == TASK_RUNNING)
217 return 0;
218
219 stack_page = (unsigned long)p;
220 fp = p->thread.usp;
221 do {
222 if (fp < stack_page + sizeof(struct thread_info) ||
223 fp >= 8184 + stack_page)
224 return 0;
225 pc = ((unsigned long *)fp)[1];
226 if (!in_sched_functions(pc))
227 return pc;
228 fp = *(unsigned long *)fp;
229 }
230 while (count++ < 16);
231 return 0;
232}
233
Bernd Schmidt7adfb582007-06-21 11:34:16 +0800234void finish_atomic_sections (struct pt_regs *regs)
235{
236 if (regs->pc < ATOMIC_SEQS_START || regs->pc >= ATOMIC_SEQS_END)
237 return;
238
239 switch (regs->pc) {
240 case ATOMIC_XCHG32 + 2:
241 put_user(regs->r1, (int *)regs->p0);
242 regs->pc += 2;
243 break;
244
245 case ATOMIC_CAS32 + 2:
246 case ATOMIC_CAS32 + 4:
247 if (regs->r0 == regs->r1)
248 put_user(regs->r2, (int *)regs->p0);
249 regs->pc = ATOMIC_CAS32 + 8;
250 break;
251 case ATOMIC_CAS32 + 6:
252 put_user(regs->r2, (int *)regs->p0);
253 regs->pc += 2;
254 break;
255
256 case ATOMIC_ADD32 + 2:
257 regs->r0 = regs->r1 + regs->r0;
258 /* fall through */
259 case ATOMIC_ADD32 + 4:
260 put_user(regs->r0, (int *)regs->p0);
261 regs->pc = ATOMIC_ADD32 + 6;
262 break;
263
264 case ATOMIC_SUB32 + 2:
265 regs->r0 = regs->r1 - regs->r0;
266 /* fall through */
267 case ATOMIC_SUB32 + 4:
268 put_user(regs->r0, (int *)regs->p0);
269 regs->pc = ATOMIC_SUB32 + 6;
270 break;
271
272 case ATOMIC_IOR32 + 2:
273 regs->r0 = regs->r1 | regs->r0;
274 /* fall through */
275 case ATOMIC_IOR32 + 4:
276 put_user(regs->r0, (int *)regs->p0);
277 regs->pc = ATOMIC_IOR32 + 6;
278 break;
279
280 case ATOMIC_AND32 + 2:
281 regs->r0 = regs->r1 & regs->r0;
282 /* fall through */
283 case ATOMIC_AND32 + 4:
284 put_user(regs->r0, (int *)regs->p0);
285 regs->pc = ATOMIC_AND32 + 6;
286 break;
287
288 case ATOMIC_XOR32 + 2:
289 regs->r0 = regs->r1 ^ regs->r0;
290 /* fall through */
291 case ATOMIC_XOR32 + 4:
292 put_user(regs->r0, (int *)regs->p0);
293 regs->pc = ATOMIC_XOR32 + 6;
294 break;
295 }
296}
297
Bryan Wu1394f032007-05-06 14:50:22 -0700298#if defined(CONFIG_ACCESS_CHECK)
Robin Getzb03b08b2007-12-23 22:57:01 +0800299/* Return 1 if access to memory range is OK, 0 otherwise */
Bryan Wu1394f032007-05-06 14:50:22 -0700300int _access_ok(unsigned long addr, unsigned long size)
301{
Bernd Schmidtbc41bb12007-10-10 17:54:19 +0800302 if (size == 0)
303 return 1;
Bryan Wu1394f032007-05-06 14:50:22 -0700304 if (addr > (addr + size))
305 return 0;
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800306 if (segment_eq(get_fs(), KERNEL_DS))
Bryan Wu1394f032007-05-06 14:50:22 -0700307 return 1;
308#ifdef CONFIG_MTD_UCLINUX
309 if (addr >= memory_start && (addr + size) <= memory_end)
310 return 1;
311 if (addr >= memory_mtd_end && (addr + size) <= physical_mem_end)
312 return 1;
313#else
314 if (addr >= memory_start && (addr + size) <= physical_mem_end)
315 return 1;
316#endif
317 if (addr >= (unsigned long)__init_begin &&
318 addr + size <= (unsigned long)__init_end)
319 return 1;
320 if (addr >= L1_SCRATCH_START
321 && addr + size <= L1_SCRATCH_START + L1_SCRATCH_LENGTH)
322 return 1;
323#if L1_CODE_LENGTH != 0
324 if (addr >= L1_CODE_START + (_etext_l1 - _stext_l1)
325 && addr + size <= L1_CODE_START + L1_CODE_LENGTH)
326 return 1;
327#endif
328#if L1_DATA_A_LENGTH != 0
329 if (addr >= L1_DATA_A_START + (_ebss_l1 - _sdata_l1)
330 && addr + size <= L1_DATA_A_START + L1_DATA_A_LENGTH)
331 return 1;
332#endif
333#if L1_DATA_B_LENGTH != 0
334 if (addr >= L1_DATA_B_START
335 && addr + size <= L1_DATA_B_START + L1_DATA_B_LENGTH)
336 return 1;
337#endif
338 return 0;
339}
340EXPORT_SYMBOL(_access_ok);
341#endif /* CONFIG_ACCESS_CHECK */