Replace <asm/uaccess.h> with <linux/uaccess.h> globally
[linux-block.git] / arch / xtensa / kernel / process.c
CommitLineData
5a0015d6
CZ
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
5a0015d6
CZ
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>
5a0015d6
CZ
23#include <linux/stddef.h>
24#include <linux/unistd.h>
25#include <linux/ptrace.h>
5a0015d6 26#include <linux/elf.h>
c91e02bd 27#include <linux/hw_breakpoint.h>
5a0015d6
CZ
28#include <linux/init.h>
29#include <linux/prctl.h>
30#include <linux/init_task.h>
31#include <linux/module.h>
32#include <linux/mqueue.h>
73089cbf 33#include <linux/fs.h>
5a0e3ad6 34#include <linux/slab.h>
11ad47a0 35#include <linux/rcupdate.h>
5a0015d6
CZ
36
37#include <asm/pgtable.h>
7c0f6ba6 38#include <linux/uaccess.h>
5a0015d6
CZ
39#include <asm/io.h>
40#include <asm/processor.h>
41#include <asm/platform.h>
42#include <asm/mmu.h>
43#include <asm/irq.h>
60063497 44#include <linux/atomic.h>
0013a854 45#include <asm/asm-offsets.h>
173d6681 46#include <asm/regs.h>
c91e02bd 47#include <asm/hw_breakpoint.h>
5a0015d6
CZ
48
49extern void ret_from_fork(void);
3306a726 50extern void ret_from_kernel_thread(void);
5a0015d6 51
5a0015d6
CZ
52struct task_struct *current_set[NR_CPUS] = {&init_task, };
53
47f3fc94
AB
54void (*pm_power_off)(void) = NULL;
55EXPORT_SYMBOL(pm_power_off);
56
5a0015d6 57
c658eac6
CZ
58#if XTENSA_HAVE_COPROCESSORS
59
60void coprocessor_release_all(struct thread_info *ti)
61{
62 unsigned long cpenable;
63 int i;
64
65 /* Make sure we don't switch tasks during this operation. */
66
67 preempt_disable();
68
69 /* Walk through all cp owners and release it for the requested one. */
70
71 cpenable = ti->cpenable;
72
73 for (i = 0; i < XCHAL_CP_MAX; i++) {
74 if (coprocessor_owner[i] == ti) {
75 coprocessor_owner[i] = 0;
76 cpenable &= ~(1 << i);
77 }
78 }
79
80 ti->cpenable = cpenable;
81 coprocessor_clear_cpenable();
82
83 preempt_enable();
84}
85
86void coprocessor_flush_all(struct thread_info *ti)
87{
88 unsigned long cpenable;
89 int i;
90
91 preempt_disable();
92
93 cpenable = ti->cpenable;
94
95 for (i = 0; i < XCHAL_CP_MAX; i++) {
96 if ((cpenable & 1) != 0 && coprocessor_owner[i] == ti)
97 coprocessor_flush(ti, i);
98 cpenable >>= 1;
99 }
100
101 preempt_enable();
102}
103
104#endif
105
106
5a0015d6
CZ
107/*
108 * Powermanagement idle function, if any is provided by the platform.
109 */
f4e2e9a4 110void arch_cpu_idle(void)
5a0015d6 111{
f4e2e9a4 112 platform_idle();
5a0015d6
CZ
113}
114
115/*
c658eac6 116 * This is called when the thread calls exit().
5a0015d6 117 */
e6464694 118void exit_thread(struct task_struct *tsk)
5a0015d6 119{
c658eac6 120#if XTENSA_HAVE_COPROCESSORS
e6464694 121 coprocessor_release_all(task_thread_info(tsk));
c658eac6 122#endif
5a0015d6
CZ
123}
124
c658eac6
CZ
125/*
126 * Flush thread state. This is called when a thread does an execve()
127 * Note that we flush coprocessor registers for the case execve fails.
128 */
5a0015d6
CZ
129void flush_thread(void)
130{
c658eac6
CZ
131#if XTENSA_HAVE_COPROCESSORS
132 struct thread_info *ti = current_thread_info();
133 coprocessor_flush_all(ti);
134 coprocessor_release_all(ti);
135#endif
c91e02bd 136 flush_ptrace_hw_breakpoint(current);
c658eac6
CZ
137}
138
139/*
55ccf3fe
SS
140 * this gets called so that we can store coprocessor state into memory and
141 * copy the current task into the new thread.
c658eac6 142 */
55ccf3fe 143int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
c658eac6
CZ
144{
145#if XTENSA_HAVE_COPROCESSORS
55ccf3fe 146 coprocessor_flush_all(task_thread_info(src));
c658eac6 147#endif
55ccf3fe
SS
148 *dst = *src;
149 return 0;
5a0015d6
CZ
150}
151
152/*
153 * Copy thread.
154 *
3306a726
MF
155 * There are two modes in which this function is called:
156 * 1) Userspace thread creation,
157 * regs != NULL, usp_thread_fn is userspace stack pointer.
158 * It is expected to copy parent regs (in case CLONE_VM is not set
159 * in the clone_flags) and set up passed usp in the childregs.
160 * 2) Kernel thread creation,
161 * regs == NULL, usp_thread_fn is the function to run in the new thread
162 * and thread_fn_arg is its parameter.
163 * childregs are not used for the kernel threads.
164 *
5a0015d6
CZ
165 * The stack layout for the new thread looks like this:
166 *
3306a726 167 * +------------------------+
5a0015d6
CZ
168 * | childregs |
169 * +------------------------+ <- thread.sp = sp in dummy-frame
170 * | dummy-frame | (saved in dummy-frame spill-area)
171 * +------------------------+
172 *
3306a726
MF
173 * We create a dummy frame to return to either ret_from_fork or
174 * ret_from_kernel_thread:
175 * a0 points to ret_from_fork/ret_from_kernel_thread (simulating a call4)
5a0015d6 176 * sp points to itself (thread.sp)
3306a726
MF
177 * a2, a3 are unused for userspace threads,
178 * a2 points to thread_fn, a3 holds thread_fn arg for kernel threads.
5a0015d6
CZ
179 *
180 * Note: This is a pristine frame, so we don't need any spill region on top of
181 * childregs.
84ed3053
MG
182 *
183 * The fun part: if we're keeping the same VM (i.e. cloning a thread,
184 * not an entire process), we're normally given a new usp, and we CANNOT share
185 * any live address register windows. If we just copy those live frames over,
186 * the two threads (parent and child) will overflow the same frames onto the
187 * parent stack at different times, likely corrupting the parent stack (esp.
188 * if the parent returns from functions that called clone() and calls new
189 * ones, before the child overflows its now old copies of its parent windows).
190 * One solution is to spill windows to the parent stack, but that's fairly
191 * involved. Much simpler to just not copy those live frames across.
5a0015d6
CZ
192 */
193
3306a726 194int copy_thread(unsigned long clone_flags, unsigned long usp_thread_fn,
afa86fc4 195 unsigned long thread_fn_arg, struct task_struct *p)
5a0015d6 196{
3306a726 197 struct pt_regs *childregs = task_pt_regs(p);
5a0015d6 198
39070cb8
CZ
199#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
200 struct thread_info *ti;
201#endif
202
5a0015d6
CZ
203 /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
204 *((int*)childregs - 3) = (unsigned long)childregs;
205 *((int*)childregs - 4) = 0;
206
5a0015d6 207 p->thread.sp = (unsigned long)childregs;
c658eac6 208
3306a726
MF
209 if (!(p->flags & PF_KTHREAD)) {
210 struct pt_regs *regs = current_pt_regs();
211 unsigned long usp = usp_thread_fn ?
212 usp_thread_fn : regs->areg[1];
213
214 p->thread.ra = MAKE_RA_FOR_CALL(
215 (unsigned long)ret_from_fork, 0x1);
5a0015d6 216
3306a726
MF
217 /* This does not copy all the regs.
218 * In a bout of brilliance or madness,
219 * ARs beyond a0-a15 exist past the end of the struct.
220 */
221 *childregs = *regs;
5a0015d6 222 childregs->areg[1] = usp;
3306a726 223 childregs->areg[2] = 0;
6ebe7da2
CZ
224
225 /* When sharing memory with the parent thread, the child
226 usually starts on a pristine stack, so we have to reset
227 windowbase, windowstart and wmask.
228 (Note that such a new thread is required to always create
229 an initial call4 frame)
230 The exception is vfork, where the new thread continues to
231 run on the parent's stack until it calls execve. This could
232 be a call8 or call12, which requires a legal stack frame
233 of the previous caller for the overflow handlers to work.
234 (Note that it's always legal to overflow live registers).
235 In this case, ensure to spill at least the stack pointer
236 of that frame. */
237
84ed3053 238 if (clone_flags & CLONE_VM) {
6ebe7da2
CZ
239 /* check that caller window is live and same stack */
240 int len = childregs->wmask & ~0xf;
241 if (regs->areg[1] == usp && len != 0) {
242 int callinc = (regs->areg[0] >> 30) & 3;
243 int caller_ars = XCHAL_NUM_AREGS - callinc * 4;
244 put_user(regs->areg[caller_ars+1],
245 (unsigned __user*)(usp - 12));
246 }
247 childregs->wmask = 1;
248 childregs->windowstart = 1;
249 childregs->windowbase = 0;
84ed3053
MG
250 } else {
251 int len = childregs->wmask & ~0xf;
252 memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
253 &regs->areg[XCHAL_NUM_AREGS - len/4], len);
254 }
c50842df
CZ
255
256 /* The thread pointer is passed in the '4th argument' (= a5) */
5a0015d6 257 if (clone_flags & CLONE_SETTLS)
c50842df 258 childregs->threadptr = childregs->areg[5];
5a0015d6 259 } else {
3306a726
MF
260 p->thread.ra = MAKE_RA_FOR_CALL(
261 (unsigned long)ret_from_kernel_thread, 1);
262
263 /* pass parameters to ret_from_kernel_thread:
264 * a2 = thread_fn, a3 = thread_fn arg
265 */
266 *((int *)childregs - 1) = thread_fn_arg;
267 *((int *)childregs - 2) = usp_thread_fn;
268
269 /* Childregs are only used when we're going to userspace
270 * in which case start_thread will set them up.
271 */
5a0015d6 272 }
c658eac6
CZ
273
274#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
275 ti = task_thread_info(p);
276 ti->cpenable = 0;
277#endif
278
c91e02bd
MF
279 clear_ptrace_hw_breakpoint(p);
280
5a0015d6
CZ
281 return 0;
282}
283
284
5a0015d6
CZ
285/*
286 * These bracket the sleeping functions..
287 */
288
289unsigned long get_wchan(struct task_struct *p)
290{
291 unsigned long sp, pc;
04fe6faf 292 unsigned long stack_page = (unsigned long) task_stack_page(p);
5a0015d6
CZ
293 int count = 0;
294
295 if (!p || p == current || p->state == TASK_RUNNING)
296 return 0;
297
298 sp = p->thread.sp;
299 pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
300
301 do {
302 if (sp < stack_page + sizeof(struct task_struct) ||
303 sp >= (stack_page + THREAD_SIZE) ||
304 pc == 0)
305 return 0;
306 if (!in_sched_functions(pc))
307 return pc;
308
309 /* Stack layout: sp-4: ra, sp-3: sp' */
310
311 pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
312 sp = *(unsigned long *)sp - 3;
313 } while (count++ < 16);
314 return 0;
315}
316
317/*
5a0015d6
CZ
318 * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
319 * of processor registers. Besides different ordering,
320 * xtensa_gregset_t contains non-live register information that
321 * 'struct pt_regs' does not. Exception handling (primarily) uses
322 * 'struct pt_regs'. Core files and ptrace use xtensa_gregset_t.
323 *
324 */
325
c658eac6 326void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
5a0015d6 327{
c658eac6
CZ
328 unsigned long wb, ws, wm;
329 int live, last;
330
331 wb = regs->windowbase;
332 ws = regs->windowstart;
333 wm = regs->wmask;
334 ws = ((ws >> wb) | (ws << (WSBITS - wb))) & ((1 << WSBITS) - 1);
335
336 /* Don't leak any random bits. */
337
688bb415 338 memset(elfregs, 0, sizeof(*elfregs));
c658eac6 339
5a0015d6
CZ
340 /* Note: PS.EXCM is not set while user task is running; its
341 * being set in regs->ps is for exception handling convenience.
342 */
343
344 elfregs->pc = regs->pc;
173d6681 345 elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT));
5a0015d6
CZ
346 elfregs->lbeg = regs->lbeg;
347 elfregs->lend = regs->lend;
348 elfregs->lcount = regs->lcount;
349 elfregs->sar = regs->sar;
c658eac6 350 elfregs->windowstart = ws;
5a0015d6 351
c658eac6
CZ
352 live = (wm & 2) ? 4 : (wm & 4) ? 8 : (wm & 8) ? 12 : 16;
353 last = XCHAL_NUM_AREGS - (wm >> 4) * 4;
354 memcpy(elfregs->a, regs->areg, live * 4);
355 memcpy(elfregs->a + last, regs->areg + last, (wm >> 4) * 16);
5a0015d6
CZ
356}
357
c658eac6 358int dump_fpu(void)
5a0015d6 359{
5a0015d6
CZ
360 return 0;
361}