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