]> Pileus Git - ~andy/linux/blob - arch/blackfin/kernel/process.c
[Blackfin] arch: initial generic time and clock sources
[~andy/linux] / 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/uaccess.h>
35 #include <linux/sched.h>
36 #include <linux/tick.h>
37 #include <linux/fs.h>
38 #include <linux/err.h>
39
40 #include <asm/blackfin.h>
41 #include <asm/fixed_code.h>
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  * The idle loop on BFIN
72  */
73 #ifdef CONFIG_IDLE_L1
74 static void default_idle(void)__attribute__((l1_text));
75 void cpu_idle(void)__attribute__((l1_text));
76 #endif
77
78 /*
79  * This is our default idle handler.  We need to disable
80  * interrupts here to ensure we don't miss a wakeup call.
81  */
82 static void default_idle(void)
83 {
84         local_irq_disable();
85         if (!need_resched())
86                 idle_with_irq_disabled();
87
88         local_irq_enable();
89 }
90
91 /*
92  * The idle thread.  We try to conserve power, while trying to keep
93  * overall latency low.  The architecture specific idle is passed
94  * a value to indicate the level of "idleness" of the system.
95  */
96 void cpu_idle(void)
97 {
98         /* endless idle loop with no priority at all */
99         while (1) {
100                 void (*idle)(void) = pm_idle;
101
102 #ifdef CONFIG_HOTPLUG_CPU
103                 if (cpu_is_offline(smp_processor_id()))
104                         cpu_die();
105 #endif
106                 if (!idle)
107                         idle = default_idle;
108                 tick_nohz_stop_sched_tick();
109                 while (!need_resched())
110                         idle();
111                 tick_nohz_restart_sched_tick();
112                 preempt_enable_no_resched();
113                 schedule();
114                 preempt_disable();
115         }
116 }
117
118 /* Fill in the fpu structure for a core dump.  */
119
120 int dump_fpu(struct pt_regs *regs, elf_fpregset_t * fpregs)
121 {
122         return 1;
123 }
124
125 /*
126  * This gets run with P1 containing the
127  * function to call, and R1 containing
128  * the "args".  Note P0 is clobbered on the way here.
129  */
130 void kernel_thread_helper(void);
131 __asm__(".section .text\n"
132         ".align 4\n"
133         "_kernel_thread_helper:\n\t"
134         "\tsp += -12;\n\t"
135         "\tr0 = r1;\n\t" "\tcall (p1);\n\t" "\tcall _do_exit;\n" ".previous");
136
137 /*
138  * Create a kernel thread.
139  */
140 pid_t kernel_thread(int (*fn) (void *), void *arg, unsigned long flags)
141 {
142         struct pt_regs regs;
143
144         memset(&regs, 0, sizeof(regs));
145
146         regs.r1 = (unsigned long)arg;
147         regs.p1 = (unsigned long)fn;
148         regs.pc = (unsigned long)kernel_thread_helper;
149         regs.orig_p0 = -1;
150         /* Set bit 2 to tell ret_from_fork we should be returning to kernel
151            mode.  */
152         regs.ipend = 0x8002;
153         __asm__ __volatile__("%0 = syscfg;":"=da"(regs.syscfg):);
154         return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL,
155                        NULL);
156 }
157
158 void flush_thread(void)
159 {
160 }
161
162 asmlinkage int bfin_vfork(struct pt_regs *regs)
163 {
164         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL,
165                        NULL);
166 }
167
168 asmlinkage int bfin_clone(struct pt_regs *regs)
169 {
170         unsigned long clone_flags;
171         unsigned long newsp;
172
173         /* syscall2 puts clone_flags in r0 and usp in r1 */
174         clone_flags = regs->r0;
175         newsp = regs->r1;
176         if (!newsp)
177                 newsp = rdusp();
178         else
179                 newsp -= 12;
180         return do_fork(clone_flags, newsp, regs, 0, NULL, NULL);
181 }
182
183 int
184 copy_thread(int nr, unsigned long clone_flags,
185             unsigned long usp, unsigned long topstk,
186             struct task_struct *p, struct pt_regs *regs)
187 {
188         struct pt_regs *childregs;
189
190         childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
191         *childregs = *regs;
192         childregs->r0 = 0;
193
194         p->thread.usp = usp;
195         p->thread.ksp = (unsigned long)childregs;
196         p->thread.pc = (unsigned long)ret_from_fork;
197
198         return 0;
199 }
200
201 /*
202  * sys_execve() executes a new program.
203  */
204
205 asmlinkage int sys_execve(char *name, char **argv, char **envp)
206 {
207         int error;
208         char *filename;
209         struct pt_regs *regs = (struct pt_regs *)((&name) + 6);
210
211         lock_kernel();
212         filename = getname(name);
213         error = PTR_ERR(filename);
214         if (IS_ERR(filename))
215                 goto out;
216         error = do_execve(filename, argv, envp, regs);
217         putname(filename);
218  out:
219         unlock_kernel();
220         return error;
221 }
222
223 unsigned long get_wchan(struct task_struct *p)
224 {
225         unsigned long fp, pc;
226         unsigned long stack_page;
227         int count = 0;
228         if (!p || p == current || p->state == TASK_RUNNING)
229                 return 0;
230
231         stack_page = (unsigned long)p;
232         fp = p->thread.usp;
233         do {
234                 if (fp < stack_page + sizeof(struct thread_info) ||
235                     fp >= 8184 + stack_page)
236                         return 0;
237                 pc = ((unsigned long *)fp)[1];
238                 if (!in_sched_functions(pc))
239                         return pc;
240                 fp = *(unsigned long *)fp;
241         }
242         while (count++ < 16);
243         return 0;
244 }
245
246 void finish_atomic_sections (struct pt_regs *regs)
247 {
248         if (regs->pc < ATOMIC_SEQS_START || regs->pc >= ATOMIC_SEQS_END)
249                 return;
250
251         switch (regs->pc) {
252         case ATOMIC_XCHG32 + 2:
253                 put_user(regs->r1, (int *)regs->p0);
254                 regs->pc += 2;
255                 break;
256
257         case ATOMIC_CAS32 + 2:
258         case ATOMIC_CAS32 + 4:
259                 if (regs->r0 == regs->r1)
260                         put_user(regs->r2, (int *)regs->p0);
261                 regs->pc = ATOMIC_CAS32 + 8;
262                 break;
263         case ATOMIC_CAS32 + 6:
264                 put_user(regs->r2, (int *)regs->p0);
265                 regs->pc += 2;
266                 break;
267
268         case ATOMIC_ADD32 + 2:
269                 regs->r0 = regs->r1 + regs->r0;
270                 /* fall through */
271         case ATOMIC_ADD32 + 4:
272                 put_user(regs->r0, (int *)regs->p0);
273                 regs->pc = ATOMIC_ADD32 + 6;
274                 break;
275
276         case ATOMIC_SUB32 + 2:
277                 regs->r0 = regs->r1 - regs->r0;
278                 /* fall through */
279         case ATOMIC_SUB32 + 4:
280                 put_user(regs->r0, (int *)regs->p0);
281                 regs->pc = ATOMIC_SUB32 + 6;
282                 break;
283
284         case ATOMIC_IOR32 + 2:
285                 regs->r0 = regs->r1 | regs->r0;
286                 /* fall through */
287         case ATOMIC_IOR32 + 4:
288                 put_user(regs->r0, (int *)regs->p0);
289                 regs->pc = ATOMIC_IOR32 + 6;
290                 break;
291
292         case ATOMIC_AND32 + 2:
293                 regs->r0 = regs->r1 & regs->r0;
294                 /* fall through */
295         case ATOMIC_AND32 + 4:
296                 put_user(regs->r0, (int *)regs->p0);
297                 regs->pc = ATOMIC_AND32 + 6;
298                 break;
299
300         case ATOMIC_XOR32 + 2:
301                 regs->r0 = regs->r1 ^ regs->r0;
302                 /* fall through */
303         case ATOMIC_XOR32 + 4:
304                 put_user(regs->r0, (int *)regs->p0);
305                 regs->pc = ATOMIC_XOR32 + 6;
306                 break;
307         }
308 }
309
310 #if defined(CONFIG_ACCESS_CHECK)
311 /* Return 1 if access to memory range is OK, 0 otherwise */
312 int _access_ok(unsigned long addr, unsigned long size)
313 {
314         if (size == 0)
315                 return 1;
316         if (addr > (addr + size))
317                 return 0;
318         if (segment_eq(get_fs(), KERNEL_DS))
319                 return 1;
320 #ifdef CONFIG_MTD_UCLINUX
321         if (addr >= memory_start && (addr + size) <= memory_end)
322                 return 1;
323         if (addr >= memory_mtd_end && (addr + size) <= physical_mem_end)
324                 return 1;
325 #else
326         if (addr >= memory_start && (addr + size) <= physical_mem_end)
327                 return 1;
328 #endif
329         if (addr >= (unsigned long)__init_begin &&
330             addr + size <= (unsigned long)__init_end)
331                 return 1;
332         if (addr >= L1_SCRATCH_START
333             && addr + size <= L1_SCRATCH_START + L1_SCRATCH_LENGTH)
334                 return 1;
335 #if L1_CODE_LENGTH != 0
336         if (addr >= L1_CODE_START + (_etext_l1 - _stext_l1)
337             && addr + size <= L1_CODE_START + L1_CODE_LENGTH)
338                 return 1;
339 #endif
340 #if L1_DATA_A_LENGTH != 0
341         if (addr >= L1_DATA_A_START + (_ebss_l1 - _sdata_l1)
342             && addr + size <= L1_DATA_A_START + L1_DATA_A_LENGTH)
343                 return 1;
344 #endif
345 #if L1_DATA_B_LENGTH != 0
346         if (addr >= L1_DATA_B_START
347             && addr + size <= L1_DATA_B_START + L1_DATA_B_LENGTH)
348                 return 1;
349 #endif
350         return 0;
351 }
352 EXPORT_SYMBOL(_access_ok);
353 #endif /* CONFIG_ACCESS_CHECK */