Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/xtensa/kernel/process.c |
| 3 | * |
| 4 | * Xtensa Processor version. |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of the GNU General Public |
| 7 | * License. See the file "COPYING" in the main directory of this archive |
| 8 | * for more details. |
| 9 | * |
| 10 | * Copyright (C) 2001 - 2005 Tensilica Inc. |
| 11 | * |
| 12 | * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com> |
| 13 | * Chris Zankel <chris@zankel.net> |
| 14 | * Marc Gauthier <marc@tensilica.com, marc@alumni.uwaterloo.ca> |
| 15 | * Kevin Chea |
| 16 | */ |
| 17 | |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 18 | #include <linux/errno.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/smp.h> |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 23 | #include <linux/stddef.h> |
| 24 | #include <linux/unistd.h> |
| 25 | #include <linux/ptrace.h> |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 26 | #include <linux/elf.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/prctl.h> |
| 29 | #include <linux/init_task.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/mqueue.h> |
Chris Zankel | 73089cb | 2007-08-04 09:27:30 -0700 | [diff] [blame] | 32 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 34 | |
| 35 | #include <asm/pgtable.h> |
| 36 | #include <asm/uaccess.h> |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 37 | #include <asm/io.h> |
| 38 | #include <asm/processor.h> |
| 39 | #include <asm/platform.h> |
| 40 | #include <asm/mmu.h> |
| 41 | #include <asm/irq.h> |
Arun Sharma | 6006349 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 42 | #include <linux/atomic.h> |
Sam Ravnborg | 0013a85 | 2005-09-09 20:57:26 +0200 | [diff] [blame] | 43 | #include <asm/asm-offsets.h> |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 44 | #include <asm/regs.h> |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 45 | |
| 46 | extern void ret_from_fork(void); |
| 47 | |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 48 | struct task_struct *current_set[NR_CPUS] = {&init_task, }; |
| 49 | |
Adrian Bunk | 47f3fc9 | 2006-03-06 15:42:47 -0800 | [diff] [blame] | 50 | void (*pm_power_off)(void) = NULL; |
| 51 | EXPORT_SYMBOL(pm_power_off); |
| 52 | |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 53 | |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 54 | #if XTENSA_HAVE_COPROCESSORS |
| 55 | |
| 56 | void coprocessor_release_all(struct thread_info *ti) |
| 57 | { |
| 58 | unsigned long cpenable; |
| 59 | int i; |
| 60 | |
| 61 | /* Make sure we don't switch tasks during this operation. */ |
| 62 | |
| 63 | preempt_disable(); |
| 64 | |
| 65 | /* Walk through all cp owners and release it for the requested one. */ |
| 66 | |
| 67 | cpenable = ti->cpenable; |
| 68 | |
| 69 | for (i = 0; i < XCHAL_CP_MAX; i++) { |
| 70 | if (coprocessor_owner[i] == ti) { |
| 71 | coprocessor_owner[i] = 0; |
| 72 | cpenable &= ~(1 << i); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | ti->cpenable = cpenable; |
| 77 | coprocessor_clear_cpenable(); |
| 78 | |
| 79 | preempt_enable(); |
| 80 | } |
| 81 | |
| 82 | void coprocessor_flush_all(struct thread_info *ti) |
| 83 | { |
| 84 | unsigned long cpenable; |
| 85 | int i; |
| 86 | |
| 87 | preempt_disable(); |
| 88 | |
| 89 | cpenable = ti->cpenable; |
| 90 | |
| 91 | for (i = 0; i < XCHAL_CP_MAX; i++) { |
| 92 | if ((cpenable & 1) != 0 && coprocessor_owner[i] == ti) |
| 93 | coprocessor_flush(ti, i); |
| 94 | cpenable >>= 1; |
| 95 | } |
| 96 | |
| 97 | preempt_enable(); |
| 98 | } |
| 99 | |
| 100 | #endif |
| 101 | |
| 102 | |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 103 | /* |
| 104 | * Powermanagement idle function, if any is provided by the platform. |
| 105 | */ |
| 106 | |
| 107 | void cpu_idle(void) |
| 108 | { |
| 109 | local_irq_enable(); |
| 110 | |
| 111 | /* endless idle loop with no priority at all */ |
| 112 | while (1) { |
| 113 | while (!need_resched()) |
| 114 | platform_idle(); |
Thomas Gleixner | bd2f553 | 2011-03-21 12:33:18 +0100 | [diff] [blame] | 115 | schedule_preempt_disabled(); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | /* |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 120 | * This is called when the thread calls exit(). |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 121 | */ |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 122 | void exit_thread(void) |
| 123 | { |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 124 | #if XTENSA_HAVE_COPROCESSORS |
| 125 | coprocessor_release_all(current_thread_info()); |
| 126 | #endif |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 129 | /* |
| 130 | * Flush thread state. This is called when a thread does an execve() |
| 131 | * Note that we flush coprocessor registers for the case execve fails. |
| 132 | */ |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 133 | void flush_thread(void) |
| 134 | { |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 135 | #if XTENSA_HAVE_COPROCESSORS |
| 136 | struct thread_info *ti = current_thread_info(); |
| 137 | coprocessor_flush_all(ti); |
| 138 | coprocessor_release_all(ti); |
| 139 | #endif |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * This is called before the thread is copied. |
| 144 | */ |
| 145 | void prepare_to_copy(struct task_struct *tsk) |
| 146 | { |
| 147 | #if XTENSA_HAVE_COPROCESSORS |
| 148 | coprocessor_flush_all(task_thread_info(tsk)); |
| 149 | #endif |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Copy thread. |
| 154 | * |
| 155 | * The stack layout for the new thread looks like this: |
| 156 | * |
| 157 | * +------------------------+ <- sp in childregs (= tos) |
| 158 | * | childregs | |
| 159 | * +------------------------+ <- thread.sp = sp in dummy-frame |
| 160 | * | dummy-frame | (saved in dummy-frame spill-area) |
| 161 | * +------------------------+ |
| 162 | * |
| 163 | * We create a dummy frame to return to ret_from_fork: |
| 164 | * a0 points to ret_from_fork (simulating a call4) |
| 165 | * sp points to itself (thread.sp) |
| 166 | * a2, a3 are unused. |
| 167 | * |
| 168 | * Note: This is a pristine frame, so we don't need any spill region on top of |
| 169 | * childregs. |
| 170 | */ |
| 171 | |
Alexey Dobriyan | 6f2c55b | 2009-04-02 16:56:59 -0700 | [diff] [blame] | 172 | int copy_thread(unsigned long clone_flags, unsigned long usp, |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 173 | unsigned long unused, |
| 174 | struct task_struct * p, struct pt_regs * regs) |
| 175 | { |
| 176 | struct pt_regs *childregs; |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 177 | struct thread_info *ti; |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 178 | unsigned long tos; |
| 179 | int user_mode = user_mode(regs); |
| 180 | |
| 181 | /* Set up new TSS. */ |
Al Viro | 04fe6fa | 2006-01-12 01:05:50 -0800 | [diff] [blame] | 182 | tos = (unsigned long)task_stack_page(p) + THREAD_SIZE; |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 183 | if (user_mode) |
| 184 | childregs = (struct pt_regs*)(tos - PT_USER_SIZE); |
| 185 | else |
| 186 | childregs = (struct pt_regs*)tos - 1; |
| 187 | |
| 188 | *childregs = *regs; |
| 189 | |
| 190 | /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */ |
| 191 | *((int*)childregs - 3) = (unsigned long)childregs; |
| 192 | *((int*)childregs - 4) = 0; |
| 193 | |
| 194 | childregs->areg[1] = tos; |
| 195 | childregs->areg[2] = 0; |
| 196 | p->set_child_tid = p->clear_child_tid = NULL; |
| 197 | p->thread.ra = MAKE_RA_FOR_CALL((unsigned long)ret_from_fork, 0x1); |
| 198 | p->thread.sp = (unsigned long)childregs; |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 199 | |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 200 | if (user_mode(regs)) { |
| 201 | |
| 202 | int len = childregs->wmask & ~0xf; |
| 203 | childregs->areg[1] = usp; |
| 204 | memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4], |
| 205 | ®s->areg[XCHAL_NUM_AREGS - len/4], len); |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 206 | // FIXME: we need to set THREADPTR in thread_info... |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 207 | if (clone_flags & CLONE_SETTLS) |
| 208 | childregs->areg[2] = childregs->areg[6]; |
| 209 | |
| 210 | } else { |
| 211 | /* In kernel space, we start a new thread with a new stack. */ |
| 212 | childregs->wmask = 1; |
| 213 | } |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 214 | |
| 215 | #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS) |
| 216 | ti = task_thread_info(p); |
| 217 | ti->cpenable = 0; |
| 218 | #endif |
| 219 | |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | /* |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 225 | * These bracket the sleeping functions.. |
| 226 | */ |
| 227 | |
| 228 | unsigned long get_wchan(struct task_struct *p) |
| 229 | { |
| 230 | unsigned long sp, pc; |
Al Viro | 04fe6fa | 2006-01-12 01:05:50 -0800 | [diff] [blame] | 231 | unsigned long stack_page = (unsigned long) task_stack_page(p); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 232 | int count = 0; |
| 233 | |
| 234 | if (!p || p == current || p->state == TASK_RUNNING) |
| 235 | return 0; |
| 236 | |
| 237 | sp = p->thread.sp; |
| 238 | pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp); |
| 239 | |
| 240 | do { |
| 241 | if (sp < stack_page + sizeof(struct task_struct) || |
| 242 | sp >= (stack_page + THREAD_SIZE) || |
| 243 | pc == 0) |
| 244 | return 0; |
| 245 | if (!in_sched_functions(pc)) |
| 246 | return pc; |
| 247 | |
| 248 | /* Stack layout: sp-4: ra, sp-3: sp' */ |
| 249 | |
| 250 | pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp); |
| 251 | sp = *(unsigned long *)sp - 3; |
| 252 | } while (count++ < 16); |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /* |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 257 | * xtensa_gregset_t and 'struct pt_regs' are vastly different formats |
| 258 | * of processor registers. Besides different ordering, |
| 259 | * xtensa_gregset_t contains non-live register information that |
| 260 | * 'struct pt_regs' does not. Exception handling (primarily) uses |
| 261 | * 'struct pt_regs'. Core files and ptrace use xtensa_gregset_t. |
| 262 | * |
| 263 | */ |
| 264 | |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 265 | void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs) |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 266 | { |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 267 | unsigned long wb, ws, wm; |
| 268 | int live, last; |
| 269 | |
| 270 | wb = regs->windowbase; |
| 271 | ws = regs->windowstart; |
| 272 | wm = regs->wmask; |
| 273 | ws = ((ws >> wb) | (ws << (WSBITS - wb))) & ((1 << WSBITS) - 1); |
| 274 | |
| 275 | /* Don't leak any random bits. */ |
| 276 | |
| 277 | memset(elfregs, 0, sizeof (elfregs)); |
| 278 | |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 279 | /* Note: PS.EXCM is not set while user task is running; its |
| 280 | * being set in regs->ps is for exception handling convenience. |
| 281 | */ |
| 282 | |
| 283 | elfregs->pc = regs->pc; |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 284 | elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT)); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 285 | elfregs->lbeg = regs->lbeg; |
| 286 | elfregs->lend = regs->lend; |
| 287 | elfregs->lcount = regs->lcount; |
| 288 | elfregs->sar = regs->sar; |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 289 | elfregs->windowstart = ws; |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 290 | |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 291 | live = (wm & 2) ? 4 : (wm & 4) ? 8 : (wm & 8) ? 12 : 16; |
| 292 | last = XCHAL_NUM_AREGS - (wm >> 4) * 4; |
| 293 | memcpy(elfregs->a, regs->areg, live * 4); |
| 294 | memcpy(elfregs->a + last, regs->areg + last, (wm >> 4) * 16); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 297 | int dump_fpu(void) |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 298 | { |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 299 | return 0; |
| 300 | } |
Chris Zankel | fc4fb2a | 2006-12-10 02:18:52 -0800 | [diff] [blame] | 301 | |
| 302 | asmlinkage |
| 303 | long xtensa_clone(unsigned long clone_flags, unsigned long newsp, |
| 304 | void __user *parent_tid, void *child_tls, |
| 305 | void __user *child_tid, long a5, |
| 306 | struct pt_regs *regs) |
| 307 | { |
| 308 | if (!newsp) |
| 309 | newsp = regs->areg[1]; |
| 310 | return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid); |
| 311 | } |
| 312 | |
| 313 | /* |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 314 | * xtensa_execve() executes a new program. |
| 315 | */ |
Chris Zankel | fc4fb2a | 2006-12-10 02:18:52 -0800 | [diff] [blame] | 316 | |
| 317 | asmlinkage |
David Howells | d762746 | 2010-08-17 23:52:56 +0100 | [diff] [blame] | 318 | long xtensa_execve(const char __user *name, |
| 319 | const char __user *const __user *argv, |
| 320 | const char __user *const __user *envp, |
Chris Zankel | fc4fb2a | 2006-12-10 02:18:52 -0800 | [diff] [blame] | 321 | long a3, long a4, long a5, |
| 322 | struct pt_regs *regs) |
| 323 | { |
| 324 | long error; |
| 325 | char * filename; |
| 326 | |
| 327 | filename = getname(name); |
| 328 | error = PTR_ERR(filename); |
| 329 | if (IS_ERR(filename)) |
| 330 | goto out; |
Chris Zankel | fc4fb2a | 2006-12-10 02:18:52 -0800 | [diff] [blame] | 331 | error = do_execve(filename, argv, envp, regs); |
Chris Zankel | fc4fb2a | 2006-12-10 02:18:52 -0800 | [diff] [blame] | 332 | putname(filename); |
| 333 | out: |
| 334 | return error; |
| 335 | } |
| 336 | |