Merge tag 'v3.9-rc1_cns3xxx_fixes' of git://git.infradead.org/users/cbou/linux-cns3xx...
[linux-2.6/libata-dev.git] / arch / xtensa / kernel / process.c
blob5cd82e9f601c15c75e13a5be9bd44f06d8409022
1 /*
2 * arch/xtensa/kernel/process.c
4 * Xtensa Processor version.
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.
10 * Copyright (C) 2001 - 2005 Tensilica Inc.
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
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>
23 #include <linux/stddef.h>
24 #include <linux/unistd.h>
25 #include <linux/ptrace.h>
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>
32 #include <linux/fs.h>
33 #include <linux/slab.h>
34 #include <linux/rcupdate.h>
36 #include <asm/pgtable.h>
37 #include <asm/uaccess.h>
38 #include <asm/io.h>
39 #include <asm/processor.h>
40 #include <asm/platform.h>
41 #include <asm/mmu.h>
42 #include <asm/irq.h>
43 #include <linux/atomic.h>
44 #include <asm/asm-offsets.h>
45 #include <asm/regs.h>
47 extern void ret_from_fork(void);
48 extern void ret_from_kernel_thread(void);
50 struct task_struct *current_set[NR_CPUS] = {&init_task, };
52 void (*pm_power_off)(void) = NULL;
53 EXPORT_SYMBOL(pm_power_off);
56 #if XTENSA_HAVE_COPROCESSORS
58 void coprocessor_release_all(struct thread_info *ti)
60 unsigned long cpenable;
61 int i;
63 /* Make sure we don't switch tasks during this operation. */
65 preempt_disable();
67 /* Walk through all cp owners and release it for the requested one. */
69 cpenable = ti->cpenable;
71 for (i = 0; i < XCHAL_CP_MAX; i++) {
72 if (coprocessor_owner[i] == ti) {
73 coprocessor_owner[i] = 0;
74 cpenable &= ~(1 << i);
78 ti->cpenable = cpenable;
79 coprocessor_clear_cpenable();
81 preempt_enable();
84 void coprocessor_flush_all(struct thread_info *ti)
86 unsigned long cpenable;
87 int i;
89 preempt_disable();
91 cpenable = ti->cpenable;
93 for (i = 0; i < XCHAL_CP_MAX; i++) {
94 if ((cpenable & 1) != 0 && coprocessor_owner[i] == ti)
95 coprocessor_flush(ti, i);
96 cpenable >>= 1;
99 preempt_enable();
102 #endif
106 * Powermanagement idle function, if any is provided by the platform.
109 void cpu_idle(void)
111 local_irq_enable();
113 /* endless idle loop with no priority at all */
114 while (1) {
115 rcu_idle_enter();
116 while (!need_resched())
117 platform_idle();
118 rcu_idle_exit();
119 schedule_preempt_disabled();
124 * This is called when the thread calls exit().
126 void exit_thread(void)
128 #if XTENSA_HAVE_COPROCESSORS
129 coprocessor_release_all(current_thread_info());
130 #endif
134 * Flush thread state. This is called when a thread does an execve()
135 * Note that we flush coprocessor registers for the case execve fails.
137 void flush_thread(void)
139 #if XTENSA_HAVE_COPROCESSORS
140 struct thread_info *ti = current_thread_info();
141 coprocessor_flush_all(ti);
142 coprocessor_release_all(ti);
143 #endif
147 * this gets called so that we can store coprocessor state into memory and
148 * copy the current task into the new thread.
150 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
152 #if XTENSA_HAVE_COPROCESSORS
153 coprocessor_flush_all(task_thread_info(src));
154 #endif
155 *dst = *src;
156 return 0;
160 * Copy thread.
162 * There are two modes in which this function is called:
163 * 1) Userspace thread creation,
164 * regs != NULL, usp_thread_fn is userspace stack pointer.
165 * It is expected to copy parent regs (in case CLONE_VM is not set
166 * in the clone_flags) and set up passed usp in the childregs.
167 * 2) Kernel thread creation,
168 * regs == NULL, usp_thread_fn is the function to run in the new thread
169 * and thread_fn_arg is its parameter.
170 * childregs are not used for the kernel threads.
172 * The stack layout for the new thread looks like this:
174 * +------------------------+
175 * | childregs |
176 * +------------------------+ <- thread.sp = sp in dummy-frame
177 * | dummy-frame | (saved in dummy-frame spill-area)
178 * +------------------------+
180 * We create a dummy frame to return to either ret_from_fork or
181 * ret_from_kernel_thread:
182 * a0 points to ret_from_fork/ret_from_kernel_thread (simulating a call4)
183 * sp points to itself (thread.sp)
184 * a2, a3 are unused for userspace threads,
185 * a2 points to thread_fn, a3 holds thread_fn arg for kernel threads.
187 * Note: This is a pristine frame, so we don't need any spill region on top of
188 * childregs.
190 * The fun part: if we're keeping the same VM (i.e. cloning a thread,
191 * not an entire process), we're normally given a new usp, and we CANNOT share
192 * any live address register windows. If we just copy those live frames over,
193 * the two threads (parent and child) will overflow the same frames onto the
194 * parent stack at different times, likely corrupting the parent stack (esp.
195 * if the parent returns from functions that called clone() and calls new
196 * ones, before the child overflows its now old copies of its parent windows).
197 * One solution is to spill windows to the parent stack, but that's fairly
198 * involved. Much simpler to just not copy those live frames across.
201 int copy_thread(unsigned long clone_flags, unsigned long usp_thread_fn,
202 unsigned long thread_fn_arg, struct task_struct *p)
204 struct pt_regs *childregs = task_pt_regs(p);
206 #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
207 struct thread_info *ti;
208 #endif
210 /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
211 *((int*)childregs - 3) = (unsigned long)childregs;
212 *((int*)childregs - 4) = 0;
214 p->thread.sp = (unsigned long)childregs;
216 if (!(p->flags & PF_KTHREAD)) {
217 struct pt_regs *regs = current_pt_regs();
218 unsigned long usp = usp_thread_fn ?
219 usp_thread_fn : regs->areg[1];
221 p->thread.ra = MAKE_RA_FOR_CALL(
222 (unsigned long)ret_from_fork, 0x1);
224 /* This does not copy all the regs.
225 * In a bout of brilliance or madness,
226 * ARs beyond a0-a15 exist past the end of the struct.
228 *childregs = *regs;
229 childregs->areg[1] = usp;
230 childregs->areg[2] = 0;
232 /* When sharing memory with the parent thread, the child
233 usually starts on a pristine stack, so we have to reset
234 windowbase, windowstart and wmask.
235 (Note that such a new thread is required to always create
236 an initial call4 frame)
237 The exception is vfork, where the new thread continues to
238 run on the parent's stack until it calls execve. This could
239 be a call8 or call12, which requires a legal stack frame
240 of the previous caller for the overflow handlers to work.
241 (Note that it's always legal to overflow live registers).
242 In this case, ensure to spill at least the stack pointer
243 of that frame. */
245 if (clone_flags & CLONE_VM) {
246 /* check that caller window is live and same stack */
247 int len = childregs->wmask & ~0xf;
248 if (regs->areg[1] == usp && len != 0) {
249 int callinc = (regs->areg[0] >> 30) & 3;
250 int caller_ars = XCHAL_NUM_AREGS - callinc * 4;
251 put_user(regs->areg[caller_ars+1],
252 (unsigned __user*)(usp - 12));
254 childregs->wmask = 1;
255 childregs->windowstart = 1;
256 childregs->windowbase = 0;
257 } else {
258 int len = childregs->wmask & ~0xf;
259 memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
260 &regs->areg[XCHAL_NUM_AREGS - len/4], len);
263 /* The thread pointer is passed in the '4th argument' (= a5) */
264 if (clone_flags & CLONE_SETTLS)
265 childregs->threadptr = childregs->areg[5];
266 } else {
267 p->thread.ra = MAKE_RA_FOR_CALL(
268 (unsigned long)ret_from_kernel_thread, 1);
270 /* pass parameters to ret_from_kernel_thread:
271 * a2 = thread_fn, a3 = thread_fn arg
273 *((int *)childregs - 1) = thread_fn_arg;
274 *((int *)childregs - 2) = usp_thread_fn;
276 /* Childregs are only used when we're going to userspace
277 * in which case start_thread will set them up.
281 #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
282 ti = task_thread_info(p);
283 ti->cpenable = 0;
284 #endif
286 return 0;
291 * These bracket the sleeping functions..
294 unsigned long get_wchan(struct task_struct *p)
296 unsigned long sp, pc;
297 unsigned long stack_page = (unsigned long) task_stack_page(p);
298 int count = 0;
300 if (!p || p == current || p->state == TASK_RUNNING)
301 return 0;
303 sp = p->thread.sp;
304 pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
306 do {
307 if (sp < stack_page + sizeof(struct task_struct) ||
308 sp >= (stack_page + THREAD_SIZE) ||
309 pc == 0)
310 return 0;
311 if (!in_sched_functions(pc))
312 return pc;
314 /* Stack layout: sp-4: ra, sp-3: sp' */
316 pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
317 sp = *(unsigned long *)sp - 3;
318 } while (count++ < 16);
319 return 0;
323 * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
324 * of processor registers. Besides different ordering,
325 * xtensa_gregset_t contains non-live register information that
326 * 'struct pt_regs' does not. Exception handling (primarily) uses
327 * 'struct pt_regs'. Core files and ptrace use xtensa_gregset_t.
331 void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
333 unsigned long wb, ws, wm;
334 int live, last;
336 wb = regs->windowbase;
337 ws = regs->windowstart;
338 wm = regs->wmask;
339 ws = ((ws >> wb) | (ws << (WSBITS - wb))) & ((1 << WSBITS) - 1);
341 /* Don't leak any random bits. */
343 memset(elfregs, 0, sizeof(*elfregs));
345 /* Note: PS.EXCM is not set while user task is running; its
346 * being set in regs->ps is for exception handling convenience.
349 elfregs->pc = regs->pc;
350 elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT));
351 elfregs->lbeg = regs->lbeg;
352 elfregs->lend = regs->lend;
353 elfregs->lcount = regs->lcount;
354 elfregs->sar = regs->sar;
355 elfregs->windowstart = ws;
357 live = (wm & 2) ? 4 : (wm & 4) ? 8 : (wm & 8) ? 12 : 16;
358 last = XCHAL_NUM_AREGS - (wm >> 4) * 4;
359 memcpy(elfregs->a, regs->areg, live * 4);
360 memcpy(elfregs->a + last, regs->areg + last, (wm >> 4) * 16);
363 int dump_fpu(void)
365 return 0;