blob: be52fe437c1b7abac32364419f17d72963b716dd [file] [log] [blame]
Chris Zankel5a0015d2005-06-23 22:01:16 -07001/*
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 Zankel5a0015d2005-06-23 22:01:16 -070018#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 Zankel5a0015d2005-06-23 22:01:16 -070023#include <linux/stddef.h>
24#include <linux/unistd.h>
25#include <linux/ptrace.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070026#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 Zankel73089cb2007-08-04 09:27:30 -070032#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Frederic Weisbecker11ad47a2012-08-22 17:27:34 +020034#include <linux/rcupdate.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070035
36#include <asm/pgtable.h>
37#include <asm/uaccess.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070038#include <asm/io.h>
39#include <asm/processor.h>
40#include <asm/platform.h>
41#include <asm/mmu.h>
42#include <asm/irq.h>
Arun Sharma600634972011-07-26 16:09:06 -070043#include <linux/atomic.h>
Sam Ravnborg0013a852005-09-09 20:57:26 +020044#include <asm/asm-offsets.h>
Chris Zankel173d6682006-12-10 02:18:48 -080045#include <asm/regs.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070046
47extern void ret_from_fork(void);
Max Filippov3306a722012-10-25 11:10:50 +040048extern void ret_from_kernel_thread(void);
Chris Zankel5a0015d2005-06-23 22:01:16 -070049
Chris Zankel5a0015d2005-06-23 22:01:16 -070050struct task_struct *current_set[NR_CPUS] = {&init_task, };
51
Adrian Bunk47f3fc92006-03-06 15:42:47 -080052void (*pm_power_off)(void) = NULL;
53EXPORT_SYMBOL(pm_power_off);
54
Chris Zankel5a0015d2005-06-23 22:01:16 -070055
Chris Zankelc658eac2008-02-12 13:17:07 -080056#if XTENSA_HAVE_COPROCESSORS
57
58void coprocessor_release_all(struct thread_info *ti)
59{
60 unsigned long cpenable;
61 int i;
62
63 /* Make sure we don't switch tasks during this operation. */
64
65 preempt_disable();
66
67 /* Walk through all cp owners and release it for the requested one. */
68
69 cpenable = ti->cpenable;
70
71 for (i = 0; i < XCHAL_CP_MAX; i++) {
72 if (coprocessor_owner[i] == ti) {
73 coprocessor_owner[i] = 0;
74 cpenable &= ~(1 << i);
75 }
76 }
77
78 ti->cpenable = cpenable;
79 coprocessor_clear_cpenable();
80
81 preempt_enable();
82}
83
84void coprocessor_flush_all(struct thread_info *ti)
85{
86 unsigned long cpenable;
87 int i;
88
89 preempt_disable();
90
91 cpenable = ti->cpenable;
92
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;
97 }
98
99 preempt_enable();
100}
101
102#endif
103
104
Chris Zankel5a0015d2005-06-23 22:01:16 -0700105/*
106 * Powermanagement idle function, if any is provided by the platform.
107 */
108
109void cpu_idle(void)
110{
111 local_irq_enable();
112
113 /* endless idle loop with no priority at all */
114 while (1) {
Frederic Weisbecker11ad47a2012-08-22 17:27:34 +0200115 rcu_idle_enter();
Chris Zankel5a0015d2005-06-23 22:01:16 -0700116 while (!need_resched())
117 platform_idle();
Frederic Weisbecker11ad47a2012-08-22 17:27:34 +0200118 rcu_idle_exit();
Thomas Gleixnerbd2f5532011-03-21 12:33:18 +0100119 schedule_preempt_disabled();
Chris Zankel5a0015d2005-06-23 22:01:16 -0700120 }
121}
122
123/*
Chris Zankelc658eac2008-02-12 13:17:07 -0800124 * This is called when the thread calls exit().
Chris Zankel5a0015d2005-06-23 22:01:16 -0700125 */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700126void exit_thread(void)
127{
Chris Zankelc658eac2008-02-12 13:17:07 -0800128#if XTENSA_HAVE_COPROCESSORS
129 coprocessor_release_all(current_thread_info());
130#endif
Chris Zankel5a0015d2005-06-23 22:01:16 -0700131}
132
Chris Zankelc658eac2008-02-12 13:17:07 -0800133/*
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.
136 */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700137void flush_thread(void)
138{
Chris Zankelc658eac2008-02-12 13:17:07 -0800139#if XTENSA_HAVE_COPROCESSORS
140 struct thread_info *ti = current_thread_info();
141 coprocessor_flush_all(ti);
142 coprocessor_release_all(ti);
143#endif
144}
145
146/*
Suresh Siddha55ccf3f2012-05-16 15:03:51 -0700147 * this gets called so that we can store coprocessor state into memory and
148 * copy the current task into the new thread.
Chris Zankelc658eac2008-02-12 13:17:07 -0800149 */
Suresh Siddha55ccf3f2012-05-16 15:03:51 -0700150int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
Chris Zankelc658eac2008-02-12 13:17:07 -0800151{
152#if XTENSA_HAVE_COPROCESSORS
Suresh Siddha55ccf3f2012-05-16 15:03:51 -0700153 coprocessor_flush_all(task_thread_info(src));
Chris Zankelc658eac2008-02-12 13:17:07 -0800154#endif
Suresh Siddha55ccf3f2012-05-16 15:03:51 -0700155 *dst = *src;
156 return 0;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700157}
158
159/*
160 * Copy thread.
161 *
Max Filippov3306a722012-10-25 11:10:50 +0400162 * 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.
171 *
Chris Zankel5a0015d2005-06-23 22:01:16 -0700172 * The stack layout for the new thread looks like this:
173 *
Max Filippov3306a722012-10-25 11:10:50 +0400174 * +------------------------+
Chris Zankel5a0015d2005-06-23 22:01:16 -0700175 * | childregs |
176 * +------------------------+ <- thread.sp = sp in dummy-frame
177 * | dummy-frame | (saved in dummy-frame spill-area)
178 * +------------------------+
179 *
Max Filippov3306a722012-10-25 11:10:50 +0400180 * 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)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700183 * sp points to itself (thread.sp)
Max Filippov3306a722012-10-25 11:10:50 +0400184 * a2, a3 are unused for userspace threads,
185 * a2 points to thread_fn, a3 holds thread_fn arg for kernel threads.
Chris Zankel5a0015d2005-06-23 22:01:16 -0700186 *
187 * Note: This is a pristine frame, so we don't need any spill region on top of
188 * childregs.
Marc Gauthier84ed3052012-10-15 03:55:35 +0400189 *
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.
Chris Zankel5a0015d2005-06-23 22:01:16 -0700199 */
200
Max Filippov3306a722012-10-25 11:10:50 +0400201int copy_thread(unsigned long clone_flags, unsigned long usp_thread_fn,
202 unsigned long thread_fn_arg,
203 struct task_struct *p, struct pt_regs *unused)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700204{
Max Filippov3306a722012-10-25 11:10:50 +0400205 struct pt_regs *childregs = task_pt_regs(p);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700206
Chris Zankel39070cb2012-10-17 23:08:20 -0700207#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
208 struct thread_info *ti;
209#endif
210
Chris Zankel5a0015d2005-06-23 22:01:16 -0700211 /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
212 *((int*)childregs - 3) = (unsigned long)childregs;
213 *((int*)childregs - 4) = 0;
214
Chris Zankel5a0015d2005-06-23 22:01:16 -0700215 p->thread.sp = (unsigned long)childregs;
Chris Zankelc658eac2008-02-12 13:17:07 -0800216
Max Filippov3306a722012-10-25 11:10:50 +0400217 if (!(p->flags & PF_KTHREAD)) {
218 struct pt_regs *regs = current_pt_regs();
219 unsigned long usp = usp_thread_fn ?
220 usp_thread_fn : regs->areg[1];
Chris Zankel5a0015d2005-06-23 22:01:16 -0700221
Max Filippov3306a722012-10-25 11:10:50 +0400222 p->thread.ra = MAKE_RA_FOR_CALL(
223 (unsigned long)ret_from_fork, 0x1);
224
225 /* This does not copy all the regs.
226 * In a bout of brilliance or madness,
227 * ARs beyond a0-a15 exist past the end of the struct.
228 */
229 *childregs = *regs;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700230 childregs->areg[1] = usp;
Max Filippov3306a722012-10-25 11:10:50 +0400231 childregs->areg[2] = 0;
Chris Zankel6ebe7da2012-10-24 13:15:21 -0700232
233 /* When sharing memory with the parent thread, the child
234 usually starts on a pristine stack, so we have to reset
235 windowbase, windowstart and wmask.
236 (Note that such a new thread is required to always create
237 an initial call4 frame)
238 The exception is vfork, where the new thread continues to
239 run on the parent's stack until it calls execve. This could
240 be a call8 or call12, which requires a legal stack frame
241 of the previous caller for the overflow handlers to work.
242 (Note that it's always legal to overflow live registers).
243 In this case, ensure to spill at least the stack pointer
244 of that frame. */
245
Marc Gauthier84ed3052012-10-15 03:55:35 +0400246 if (clone_flags & CLONE_VM) {
Chris Zankel6ebe7da2012-10-24 13:15:21 -0700247 /* check that caller window is live and same stack */
248 int len = childregs->wmask & ~0xf;
249 if (regs->areg[1] == usp && len != 0) {
250 int callinc = (regs->areg[0] >> 30) & 3;
251 int caller_ars = XCHAL_NUM_AREGS - callinc * 4;
252 put_user(regs->areg[caller_ars+1],
253 (unsigned __user*)(usp - 12));
254 }
255 childregs->wmask = 1;
256 childregs->windowstart = 1;
257 childregs->windowbase = 0;
Marc Gauthier84ed3052012-10-15 03:55:35 +0400258 } else {
259 int len = childregs->wmask & ~0xf;
260 memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
261 &regs->areg[XCHAL_NUM_AREGS - len/4], len);
262 }
Chris Zankelc658eac2008-02-12 13:17:07 -0800263// FIXME: we need to set THREADPTR in thread_info...
Chris Zankel5a0015d2005-06-23 22:01:16 -0700264 if (clone_flags & CLONE_SETTLS)
265 childregs->areg[2] = childregs->areg[6];
Chris Zankel5a0015d2005-06-23 22:01:16 -0700266 } else {
Max Filippov3306a722012-10-25 11:10:50 +0400267 p->thread.ra = MAKE_RA_FOR_CALL(
268 (unsigned long)ret_from_kernel_thread, 1);
269
270 /* pass parameters to ret_from_kernel_thread:
271 * a2 = thread_fn, a3 = thread_fn arg
272 */
273 *((int *)childregs - 1) = thread_fn_arg;
274 *((int *)childregs - 2) = usp_thread_fn;
275
276 /* Childregs are only used when we're going to userspace
277 * in which case start_thread will set them up.
278 */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700279 }
Chris Zankelc658eac2008-02-12 13:17:07 -0800280
281#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
282 ti = task_thread_info(p);
283 ti->cpenable = 0;
284#endif
285
Chris Zankel5a0015d2005-06-23 22:01:16 -0700286 return 0;
287}
288
289
290/*
Chris Zankel5a0015d2005-06-23 22:01:16 -0700291 * These bracket the sleeping functions..
292 */
293
294unsigned long get_wchan(struct task_struct *p)
295{
296 unsigned long sp, pc;
Al Viro04fe6fa2006-01-12 01:05:50 -0800297 unsigned long stack_page = (unsigned long) task_stack_page(p);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700298 int count = 0;
299
300 if (!p || p == current || p->state == TASK_RUNNING)
301 return 0;
302
303 sp = p->thread.sp;
304 pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
305
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;
313
314 /* Stack layout: sp-4: ra, sp-3: sp' */
315
316 pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
317 sp = *(unsigned long *)sp - 3;
318 } while (count++ < 16);
319 return 0;
320}
321
322/*
Chris Zankel5a0015d2005-06-23 22:01:16 -0700323 * 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.
328 *
329 */
330
Chris Zankelc658eac2008-02-12 13:17:07 -0800331void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700332{
Chris Zankelc658eac2008-02-12 13:17:07 -0800333 unsigned long wb, ws, wm;
334 int live, last;
335
336 wb = regs->windowbase;
337 ws = regs->windowstart;
338 wm = regs->wmask;
339 ws = ((ws >> wb) | (ws << (WSBITS - wb))) & ((1 << WSBITS) - 1);
340
341 /* Don't leak any random bits. */
342
Alan Cox688bb412012-07-11 14:02:50 -0700343 memset(elfregs, 0, sizeof(*elfregs));
Chris Zankelc658eac2008-02-12 13:17:07 -0800344
Chris Zankel5a0015d2005-06-23 22:01:16 -0700345 /* Note: PS.EXCM is not set while user task is running; its
346 * being set in regs->ps is for exception handling convenience.
347 */
348
349 elfregs->pc = regs->pc;
Chris Zankel173d6682006-12-10 02:18:48 -0800350 elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT));
Chris Zankel5a0015d2005-06-23 22:01:16 -0700351 elfregs->lbeg = regs->lbeg;
352 elfregs->lend = regs->lend;
353 elfregs->lcount = regs->lcount;
354 elfregs->sar = regs->sar;
Chris Zankelc658eac2008-02-12 13:17:07 -0800355 elfregs->windowstart = ws;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700356
Chris Zankelc658eac2008-02-12 13:17:07 -0800357 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);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700361}
362
Chris Zankelc658eac2008-02-12 13:17:07 -0800363int dump_fpu(void)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700364{
Chris Zankel5a0015d2005-06-23 22:01:16 -0700365 return 0;
366}
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800367
368asmlinkage
369long xtensa_clone(unsigned long clone_flags, unsigned long newsp,
370 void __user *parent_tid, void *child_tls,
371 void __user *child_tid, long a5,
372 struct pt_regs *regs)
373{
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800374 return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
375}
376
377/*
Chris Zankelc658eac2008-02-12 13:17:07 -0800378 * xtensa_execve() executes a new program.
379 */
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800380
381asmlinkage
David Howellsd7627462010-08-17 23:52:56 +0100382long xtensa_execve(const char __user *name,
383 const char __user *const __user *argv,
384 const char __user *const __user *envp,
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800385 long a3, long a4, long a5,
386 struct pt_regs *regs)
387{
388 long error;
Jeff Layton91a27b22012-10-10 15:25:28 -0400389 struct filename *filename;
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800390
391 filename = getname(name);
392 error = PTR_ERR(filename);
393 if (IS_ERR(filename))
394 goto out;
Jeff Layton91a27b22012-10-10 15:25:28 -0400395 error = do_execve(filename->name, argv, envp, regs);
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800396 putname(filename);
397out:
398 return error;
399}
400