Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 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> |
Mike Frysinger | 1f83b8f | 2007-07-12 22:58:21 +0800 | [diff] [blame] | 34 | #include <linux/uaccess.h> |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 35 | #include <linux/sched.h> |
| 36 | #include <linux/tick.h> |
Bryan Wu | d31c5ab | 2007-08-10 13:00:42 -0700 | [diff] [blame] | 37 | #include <linux/fs.h> |
| 38 | #include <linux/err.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 39 | |
| 40 | #include <asm/blackfin.h> |
Bernd Schmidt | 7adfb58 | 2007-06-21 11:34:16 +0800 | [diff] [blame] | 41 | #include <asm/fixed_code.h> |
Graf Yang | dbc895f | 2009-01-07 23:14:39 +0800 | [diff] [blame^] | 42 | #include <asm/mem_map.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 43 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 44 | asmlinkage void ret_from_fork(void); |
| 45 | |
| 46 | /* Points to the SDRAM backup memory for the stack that is currently in |
| 47 | * L1 scratchpad memory. |
| 48 | */ |
| 49 | void *current_l1_stack_save; |
| 50 | |
| 51 | /* The number of tasks currently using a L1 stack area. The SRAM is |
| 52 | * allocated/deallocated whenever this changes from/to zero. |
| 53 | */ |
| 54 | int nr_l1stack_tasks; |
| 55 | |
| 56 | /* Start and length of the area in L1 scratchpad memory which we've allocated |
| 57 | * for process stacks. |
| 58 | */ |
| 59 | void *l1_stack_base; |
| 60 | unsigned long l1_stack_len; |
| 61 | |
| 62 | /* |
| 63 | * Powermanagement idle function, if any.. |
| 64 | */ |
| 65 | void (*pm_idle)(void) = NULL; |
| 66 | EXPORT_SYMBOL(pm_idle); |
| 67 | |
| 68 | void (*pm_power_off)(void) = NULL; |
| 69 | EXPORT_SYMBOL(pm_power_off); |
| 70 | |
| 71 | /* |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 72 | * The idle loop on BFIN |
| 73 | */ |
| 74 | #ifdef CONFIG_IDLE_L1 |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 75 | static void default_idle(void)__attribute__((l1_text)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 76 | void cpu_idle(void)__attribute__((l1_text)); |
| 77 | #endif |
| 78 | |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 79 | /* |
| 80 | * This is our default idle handler. We need to disable |
| 81 | * interrupts here to ensure we don't miss a wakeup call. |
| 82 | */ |
| 83 | static void default_idle(void) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 84 | { |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 85 | local_irq_disable(); |
| 86 | if (!need_resched()) |
| 87 | idle_with_irq_disabled(); |
| 88 | |
| 89 | local_irq_enable(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 92 | /* |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 93 | * The idle thread. We try to conserve power, while trying to keep |
| 94 | * overall latency low. The architecture specific idle is passed |
| 95 | * a value to indicate the level of "idleness" of the system. |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 96 | */ |
| 97 | void cpu_idle(void) |
| 98 | { |
| 99 | /* endless idle loop with no priority at all */ |
| 100 | while (1) { |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 101 | void (*idle)(void) = pm_idle; |
| 102 | |
| 103 | #ifdef CONFIG_HOTPLUG_CPU |
| 104 | if (cpu_is_offline(smp_processor_id())) |
| 105 | cpu_die(); |
| 106 | #endif |
| 107 | if (!idle) |
| 108 | idle = default_idle; |
Thomas Gleixner | b8f8c3c | 2008-07-18 17:27:28 +0200 | [diff] [blame] | 109 | tick_nohz_stop_sched_tick(1); |
Vitja Makarov | 8b5f79f | 2008-02-29 12:24:23 +0800 | [diff] [blame] | 110 | while (!need_resched()) |
| 111 | idle(); |
| 112 | tick_nohz_restart_sched_tick(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 113 | preempt_enable_no_resched(); |
| 114 | schedule(); |
| 115 | preempt_disable(); |
| 116 | } |
| 117 | } |
| 118 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 119 | /* Fill in the fpu structure for a core dump. */ |
| 120 | |
| 121 | int dump_fpu(struct pt_regs *regs, elf_fpregset_t * fpregs) |
| 122 | { |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * This gets run with P1 containing the |
| 128 | * function to call, and R1 containing |
| 129 | * the "args". Note P0 is clobbered on the way here. |
| 130 | */ |
| 131 | void kernel_thread_helper(void); |
| 132 | __asm__(".section .text\n" |
| 133 | ".align 4\n" |
| 134 | "_kernel_thread_helper:\n\t" |
| 135 | "\tsp += -12;\n\t" |
| 136 | "\tr0 = r1;\n\t" "\tcall (p1);\n\t" "\tcall _do_exit;\n" ".previous"); |
| 137 | |
| 138 | /* |
| 139 | * Create a kernel thread. |
| 140 | */ |
| 141 | pid_t kernel_thread(int (*fn) (void *), void *arg, unsigned long flags) |
| 142 | { |
| 143 | struct pt_regs regs; |
| 144 | |
| 145 | memset(®s, 0, sizeof(regs)); |
| 146 | |
| 147 | regs.r1 = (unsigned long)arg; |
| 148 | regs.p1 = (unsigned long)fn; |
| 149 | regs.pc = (unsigned long)kernel_thread_helper; |
| 150 | regs.orig_p0 = -1; |
| 151 | /* Set bit 2 to tell ret_from_fork we should be returning to kernel |
| 152 | mode. */ |
| 153 | regs.ipend = 0x8002; |
| 154 | __asm__ __volatile__("%0 = syscfg;":"=da"(regs.syscfg):); |
| 155 | return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, ®s, 0, NULL, |
| 156 | NULL); |
| 157 | } |
Mike Frysinger | fe8015c | 2008-10-28 11:07:15 +0800 | [diff] [blame] | 158 | EXPORT_SYMBOL(kernel_thread); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 159 | |
| 160 | void flush_thread(void) |
| 161 | { |
| 162 | } |
| 163 | |
| 164 | asmlinkage int bfin_vfork(struct pt_regs *regs) |
| 165 | { |
| 166 | return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL, |
| 167 | NULL); |
| 168 | } |
| 169 | |
| 170 | asmlinkage int bfin_clone(struct pt_regs *regs) |
| 171 | { |
| 172 | unsigned long clone_flags; |
| 173 | unsigned long newsp; |
| 174 | |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 175 | #ifdef __ARCH_SYNC_CORE_DCACHE |
| 176 | if (current->rt.nr_cpus_allowed == num_possible_cpus()) { |
| 177 | current->cpus_allowed = cpumask_of_cpu(smp_processor_id()); |
| 178 | current->rt.nr_cpus_allowed = 1; |
| 179 | } |
| 180 | #endif |
| 181 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 182 | /* syscall2 puts clone_flags in r0 and usp in r1 */ |
| 183 | clone_flags = regs->r0; |
| 184 | newsp = regs->r1; |
| 185 | if (!newsp) |
| 186 | newsp = rdusp(); |
| 187 | else |
| 188 | newsp -= 12; |
| 189 | return do_fork(clone_flags, newsp, regs, 0, NULL, NULL); |
| 190 | } |
| 191 | |
| 192 | int |
| 193 | copy_thread(int nr, unsigned long clone_flags, |
| 194 | unsigned long usp, unsigned long topstk, |
| 195 | struct task_struct *p, struct pt_regs *regs) |
| 196 | { |
| 197 | struct pt_regs *childregs; |
| 198 | |
| 199 | childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1; |
| 200 | *childregs = *regs; |
| 201 | childregs->r0 = 0; |
| 202 | |
| 203 | p->thread.usp = usp; |
| 204 | p->thread.ksp = (unsigned long)childregs; |
| 205 | p->thread.pc = (unsigned long)ret_from_fork; |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | /* |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 211 | * sys_execve() executes a new program. |
| 212 | */ |
| 213 | |
Mike Frysinger | 0ddeeca | 2008-03-07 02:37:41 +0800 | [diff] [blame] | 214 | asmlinkage int sys_execve(char __user *name, char __user * __user *argv, char __user * __user *envp) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 215 | { |
| 216 | int error; |
| 217 | char *filename; |
| 218 | struct pt_regs *regs = (struct pt_regs *)((&name) + 6); |
| 219 | |
| 220 | lock_kernel(); |
| 221 | filename = getname(name); |
| 222 | error = PTR_ERR(filename); |
| 223 | if (IS_ERR(filename)) |
| 224 | goto out; |
| 225 | error = do_execve(filename, argv, envp, regs); |
| 226 | putname(filename); |
Mike Frysinger | 1f83b8f | 2007-07-12 22:58:21 +0800 | [diff] [blame] | 227 | out: |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 228 | unlock_kernel(); |
| 229 | return error; |
| 230 | } |
| 231 | |
| 232 | unsigned long get_wchan(struct task_struct *p) |
| 233 | { |
| 234 | unsigned long fp, pc; |
| 235 | unsigned long stack_page; |
| 236 | int count = 0; |
| 237 | if (!p || p == current || p->state == TASK_RUNNING) |
| 238 | return 0; |
| 239 | |
| 240 | stack_page = (unsigned long)p; |
| 241 | fp = p->thread.usp; |
| 242 | do { |
| 243 | if (fp < stack_page + sizeof(struct thread_info) || |
| 244 | fp >= 8184 + stack_page) |
| 245 | return 0; |
| 246 | pc = ((unsigned long *)fp)[1]; |
| 247 | if (!in_sched_functions(pc)) |
| 248 | return pc; |
| 249 | fp = *(unsigned long *)fp; |
| 250 | } |
| 251 | while (count++ < 16); |
| 252 | return 0; |
| 253 | } |
| 254 | |
Bernd Schmidt | 7adfb58 | 2007-06-21 11:34:16 +0800 | [diff] [blame] | 255 | void finish_atomic_sections (struct pt_regs *regs) |
| 256 | { |
Bernd Schmidt | 19d6d7d | 2008-05-07 11:41:26 +0800 | [diff] [blame] | 257 | int __user *up0 = (int __user *)regs->p0; |
Mike Frysinger | 0ddeeca | 2008-03-07 02:37:41 +0800 | [diff] [blame] | 258 | |
Bernd Schmidt | 7adfb58 | 2007-06-21 11:34:16 +0800 | [diff] [blame] | 259 | if (regs->pc < ATOMIC_SEQS_START || regs->pc >= ATOMIC_SEQS_END) |
| 260 | return; |
| 261 | |
| 262 | switch (regs->pc) { |
| 263 | case ATOMIC_XCHG32 + 2: |
Mike Frysinger | 0ddeeca | 2008-03-07 02:37:41 +0800 | [diff] [blame] | 264 | put_user(regs->r1, up0); |
Bernd Schmidt | 7adfb58 | 2007-06-21 11:34:16 +0800 | [diff] [blame] | 265 | regs->pc += 2; |
| 266 | break; |
| 267 | |
| 268 | case ATOMIC_CAS32 + 2: |
| 269 | case ATOMIC_CAS32 + 4: |
| 270 | if (regs->r0 == regs->r1) |
Mike Frysinger | 0ddeeca | 2008-03-07 02:37:41 +0800 | [diff] [blame] | 271 | put_user(regs->r2, up0); |
Bernd Schmidt | 7adfb58 | 2007-06-21 11:34:16 +0800 | [diff] [blame] | 272 | regs->pc = ATOMIC_CAS32 + 8; |
| 273 | break; |
| 274 | case ATOMIC_CAS32 + 6: |
Mike Frysinger | 0ddeeca | 2008-03-07 02:37:41 +0800 | [diff] [blame] | 275 | put_user(regs->r2, up0); |
Bernd Schmidt | 7adfb58 | 2007-06-21 11:34:16 +0800 | [diff] [blame] | 276 | regs->pc += 2; |
| 277 | break; |
| 278 | |
| 279 | case ATOMIC_ADD32 + 2: |
| 280 | regs->r0 = regs->r1 + regs->r0; |
| 281 | /* fall through */ |
| 282 | case ATOMIC_ADD32 + 4: |
Mike Frysinger | 0ddeeca | 2008-03-07 02:37:41 +0800 | [diff] [blame] | 283 | put_user(regs->r0, up0); |
Bernd Schmidt | 7adfb58 | 2007-06-21 11:34:16 +0800 | [diff] [blame] | 284 | regs->pc = ATOMIC_ADD32 + 6; |
| 285 | break; |
| 286 | |
| 287 | case ATOMIC_SUB32 + 2: |
| 288 | regs->r0 = regs->r1 - regs->r0; |
| 289 | /* fall through */ |
| 290 | case ATOMIC_SUB32 + 4: |
Mike Frysinger | 0ddeeca | 2008-03-07 02:37:41 +0800 | [diff] [blame] | 291 | put_user(regs->r0, up0); |
Bernd Schmidt | 7adfb58 | 2007-06-21 11:34:16 +0800 | [diff] [blame] | 292 | regs->pc = ATOMIC_SUB32 + 6; |
| 293 | break; |
| 294 | |
| 295 | case ATOMIC_IOR32 + 2: |
| 296 | regs->r0 = regs->r1 | regs->r0; |
| 297 | /* fall through */ |
| 298 | case ATOMIC_IOR32 + 4: |
Mike Frysinger | 0ddeeca | 2008-03-07 02:37:41 +0800 | [diff] [blame] | 299 | put_user(regs->r0, up0); |
Bernd Schmidt | 7adfb58 | 2007-06-21 11:34:16 +0800 | [diff] [blame] | 300 | regs->pc = ATOMIC_IOR32 + 6; |
| 301 | break; |
| 302 | |
| 303 | case ATOMIC_AND32 + 2: |
| 304 | regs->r0 = regs->r1 & regs->r0; |
| 305 | /* fall through */ |
| 306 | case ATOMIC_AND32 + 4: |
Mike Frysinger | 0ddeeca | 2008-03-07 02:37:41 +0800 | [diff] [blame] | 307 | put_user(regs->r0, up0); |
Bernd Schmidt | 7adfb58 | 2007-06-21 11:34:16 +0800 | [diff] [blame] | 308 | regs->pc = ATOMIC_AND32 + 6; |
| 309 | break; |
| 310 | |
| 311 | case ATOMIC_XOR32 + 2: |
| 312 | regs->r0 = regs->r1 ^ regs->r0; |
| 313 | /* fall through */ |
| 314 | case ATOMIC_XOR32 + 4: |
Mike Frysinger | 0ddeeca | 2008-03-07 02:37:41 +0800 | [diff] [blame] | 315 | put_user(regs->r0, up0); |
Bernd Schmidt | 7adfb58 | 2007-06-21 11:34:16 +0800 | [diff] [blame] | 316 | regs->pc = ATOMIC_XOR32 + 6; |
| 317 | break; |
| 318 | } |
| 319 | } |
| 320 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 321 | #if defined(CONFIG_ACCESS_CHECK) |
Robin Getz | b03b08b | 2007-12-23 22:57:01 +0800 | [diff] [blame] | 322 | /* Return 1 if access to memory range is OK, 0 otherwise */ |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 323 | int _access_ok(unsigned long addr, unsigned long size) |
| 324 | { |
Bernd Schmidt | bc41bb1 | 2007-10-10 17:54:19 +0800 | [diff] [blame] | 325 | if (size == 0) |
| 326 | return 1; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 327 | if (addr > (addr + size)) |
| 328 | return 0; |
Mike Frysinger | 1f83b8f | 2007-07-12 22:58:21 +0800 | [diff] [blame] | 329 | if (segment_eq(get_fs(), KERNEL_DS)) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 330 | return 1; |
| 331 | #ifdef CONFIG_MTD_UCLINUX |
| 332 | if (addr >= memory_start && (addr + size) <= memory_end) |
| 333 | return 1; |
| 334 | if (addr >= memory_mtd_end && (addr + size) <= physical_mem_end) |
| 335 | return 1; |
Bernd Schmidt | d5adb02 | 2008-04-24 03:06:15 +0800 | [diff] [blame] | 336 | |
| 337 | #ifdef CONFIG_ROMFS_MTD_FS |
| 338 | /* For XIP, allow user space to use pointers within the ROMFS. */ |
| 339 | if (addr >= memory_mtd_start && (addr + size) <= memory_mtd_end) |
| 340 | return 1; |
| 341 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 342 | #else |
| 343 | if (addr >= memory_start && (addr + size) <= physical_mem_end) |
| 344 | return 1; |
| 345 | #endif |
| 346 | if (addr >= (unsigned long)__init_begin && |
| 347 | addr + size <= (unsigned long)__init_end) |
| 348 | return 1; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 349 | if (addr >= get_l1_scratch_start() |
| 350 | && addr + size <= get_l1_scratch_start() + L1_SCRATCH_LENGTH) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 351 | return 1; |
| 352 | #if L1_CODE_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 353 | if (addr >= get_l1_code_start() + (_etext_l1 - _stext_l1) |
| 354 | && addr + size <= get_l1_code_start() + L1_CODE_LENGTH) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 355 | return 1; |
| 356 | #endif |
| 357 | #if L1_DATA_A_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 358 | if (addr >= get_l1_data_a_start() + (_ebss_l1 - _sdata_l1) |
| 359 | && addr + size <= get_l1_data_a_start() + L1_DATA_A_LENGTH) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 360 | return 1; |
| 361 | #endif |
| 362 | #if L1_DATA_B_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 363 | if (addr >= get_l1_data_b_start() + (_ebss_b_l1 - _sdata_b_l1) |
| 364 | && addr + size <= get_l1_data_b_start() + L1_DATA_B_LENGTH) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 365 | return 1; |
| 366 | #endif |
Jie Zhang | b2c2f30 | 2008-10-28 15:57:49 +0800 | [diff] [blame] | 367 | #if L2_LENGTH != 0 |
| 368 | if (addr >= L2_START + (_ebss_l2 - _stext_l2) |
| 369 | && addr + size <= L2_START + L2_LENGTH) |
| 370 | return 1; |
| 371 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 372 | return 0; |
| 373 | } |
| 374 | EXPORT_SYMBOL(_access_ok); |
| 375 | #endif /* CONFIG_ACCESS_CHECK */ |