blob: 341c151af919eec7c3039f6398f36c9ea4e9315c [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);
48
Chris Zankel5a0015d2005-06-23 22:01:16 -070049struct task_struct *current_set[NR_CPUS] = {&init_task, };
50
Adrian Bunk47f3fc92006-03-06 15:42:47 -080051void (*pm_power_off)(void) = NULL;
52EXPORT_SYMBOL(pm_power_off);
53
Chris Zankel5a0015d2005-06-23 22:01:16 -070054
Chris Zankelc658eac2008-02-12 13:17:07 -080055#if XTENSA_HAVE_COPROCESSORS
56
57void coprocessor_release_all(struct thread_info *ti)
58{
59 unsigned long cpenable;
60 int i;
61
62 /* Make sure we don't switch tasks during this operation. */
63
64 preempt_disable();
65
66 /* Walk through all cp owners and release it for the requested one. */
67
68 cpenable = ti->cpenable;
69
70 for (i = 0; i < XCHAL_CP_MAX; i++) {
71 if (coprocessor_owner[i] == ti) {
72 coprocessor_owner[i] = 0;
73 cpenable &= ~(1 << i);
74 }
75 }
76
77 ti->cpenable = cpenable;
78 coprocessor_clear_cpenable();
79
80 preempt_enable();
81}
82
83void coprocessor_flush_all(struct thread_info *ti)
84{
85 unsigned long cpenable;
86 int i;
87
88 preempt_disable();
89
90 cpenable = ti->cpenable;
91
92 for (i = 0; i < XCHAL_CP_MAX; i++) {
93 if ((cpenable & 1) != 0 && coprocessor_owner[i] == ti)
94 coprocessor_flush(ti, i);
95 cpenable >>= 1;
96 }
97
98 preempt_enable();
99}
100
101#endif
102
103
Chris Zankel5a0015d2005-06-23 22:01:16 -0700104/*
105 * Powermanagement idle function, if any is provided by the platform.
106 */
107
108void cpu_idle(void)
109{
110 local_irq_enable();
111
112 /* endless idle loop with no priority at all */
113 while (1) {
Frederic Weisbecker11ad47a2012-08-22 17:27:34 +0200114 rcu_idle_enter();
Chris Zankel5a0015d2005-06-23 22:01:16 -0700115 while (!need_resched())
116 platform_idle();
Frederic Weisbecker11ad47a2012-08-22 17:27:34 +0200117 rcu_idle_exit();
Thomas Gleixnerbd2f5532011-03-21 12:33:18 +0100118 schedule_preempt_disabled();
Chris Zankel5a0015d2005-06-23 22:01:16 -0700119 }
120}
121
122/*
Chris Zankelc658eac2008-02-12 13:17:07 -0800123 * This is called when the thread calls exit().
Chris Zankel5a0015d2005-06-23 22:01:16 -0700124 */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700125void exit_thread(void)
126{
Chris Zankelc658eac2008-02-12 13:17:07 -0800127#if XTENSA_HAVE_COPROCESSORS
128 coprocessor_release_all(current_thread_info());
129#endif
Chris Zankel5a0015d2005-06-23 22:01:16 -0700130}
131
Chris Zankelc658eac2008-02-12 13:17:07 -0800132/*
133 * Flush thread state. This is called when a thread does an execve()
134 * Note that we flush coprocessor registers for the case execve fails.
135 */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700136void flush_thread(void)
137{
Chris Zankelc658eac2008-02-12 13:17:07 -0800138#if XTENSA_HAVE_COPROCESSORS
139 struct thread_info *ti = current_thread_info();
140 coprocessor_flush_all(ti);
141 coprocessor_release_all(ti);
142#endif
143}
144
145/*
Suresh Siddha55ccf3f2012-05-16 15:03:51 -0700146 * this gets called so that we can store coprocessor state into memory and
147 * copy the current task into the new thread.
Chris Zankelc658eac2008-02-12 13:17:07 -0800148 */
Suresh Siddha55ccf3f2012-05-16 15:03:51 -0700149int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
Chris Zankelc658eac2008-02-12 13:17:07 -0800150{
151#if XTENSA_HAVE_COPROCESSORS
Suresh Siddha55ccf3f2012-05-16 15:03:51 -0700152 coprocessor_flush_all(task_thread_info(src));
Chris Zankelc658eac2008-02-12 13:17:07 -0800153#endif
Suresh Siddha55ccf3f2012-05-16 15:03:51 -0700154 *dst = *src;
155 return 0;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700156}
157
158/*
159 * Copy thread.
160 *
161 * The stack layout for the new thread looks like this:
162 *
163 * +------------------------+ <- sp in childregs (= tos)
164 * | childregs |
165 * +------------------------+ <- thread.sp = sp in dummy-frame
166 * | dummy-frame | (saved in dummy-frame spill-area)
167 * +------------------------+
168 *
169 * We create a dummy frame to return to ret_from_fork:
170 * a0 points to ret_from_fork (simulating a call4)
171 * sp points to itself (thread.sp)
172 * a2, a3 are unused.
173 *
174 * Note: This is a pristine frame, so we don't need any spill region on top of
175 * childregs.
Marc Gauthier84ed3052012-10-15 03:55:35 +0400176 *
177 * The fun part: if we're keeping the same VM (i.e. cloning a thread,
178 * not an entire process), we're normally given a new usp, and we CANNOT share
179 * any live address register windows. If we just copy those live frames over,
180 * the two threads (parent and child) will overflow the same frames onto the
181 * parent stack at different times, likely corrupting the parent stack (esp.
182 * if the parent returns from functions that called clone() and calls new
183 * ones, before the child overflows its now old copies of its parent windows).
184 * One solution is to spill windows to the parent stack, but that's fairly
185 * involved. Much simpler to just not copy those live frames across.
Chris Zankel5a0015d2005-06-23 22:01:16 -0700186 */
187
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700188int copy_thread(unsigned long clone_flags, unsigned long usp,
Chris Zankel5a0015d2005-06-23 22:01:16 -0700189 unsigned long unused,
190 struct task_struct * p, struct pt_regs * regs)
191{
192 struct pt_regs *childregs;
193 unsigned long tos;
194 int user_mode = user_mode(regs);
195
Chris Zankel39070cb2012-10-17 23:08:20 -0700196#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
197 struct thread_info *ti;
198#endif
199
Chris Zankel5a0015d2005-06-23 22:01:16 -0700200 /* Set up new TSS. */
Al Viro04fe6fa2006-01-12 01:05:50 -0800201 tos = (unsigned long)task_stack_page(p) + THREAD_SIZE;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700202 if (user_mode)
203 childregs = (struct pt_regs*)(tos - PT_USER_SIZE);
204 else
205 childregs = (struct pt_regs*)tos - 1;
206
Marc Gauthier84ed3052012-10-15 03:55:35 +0400207 /* This does not copy all the regs. In a bout of brilliance or madness,
208 ARs beyond a0-a15 exist past the end of the struct. */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700209 *childregs = *regs;
210
211 /* 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 childregs->areg[2] = 0;
216 p->set_child_tid = p->clear_child_tid = NULL;
217 p->thread.ra = MAKE_RA_FOR_CALL((unsigned long)ret_from_fork, 0x1);
218 p->thread.sp = (unsigned long)childregs;
Chris Zankelc658eac2008-02-12 13:17:07 -0800219
Chris Zankel5a0015d2005-06-23 22:01:16 -0700220 if (user_mode(regs)) {
221
Chris Zankel5a0015d2005-06-23 22:01:16 -0700222 childregs->areg[1] = usp;
Chris Zankel6ebe7da2012-10-24 13:15:21 -0700223
224 /* When sharing memory with the parent thread, the child
225 usually starts on a pristine stack, so we have to reset
226 windowbase, windowstart and wmask.
227 (Note that such a new thread is required to always create
228 an initial call4 frame)
229 The exception is vfork, where the new thread continues to
230 run on the parent's stack until it calls execve. This could
231 be a call8 or call12, which requires a legal stack frame
232 of the previous caller for the overflow handlers to work.
233 (Note that it's always legal to overflow live registers).
234 In this case, ensure to spill at least the stack pointer
235 of that frame. */
236
Marc Gauthier84ed3052012-10-15 03:55:35 +0400237 if (clone_flags & CLONE_VM) {
Chris Zankel6ebe7da2012-10-24 13:15:21 -0700238 /* check that caller window is live and same stack */
239 int len = childregs->wmask & ~0xf;
240 if (regs->areg[1] == usp && len != 0) {
241 int callinc = (regs->areg[0] >> 30) & 3;
242 int caller_ars = XCHAL_NUM_AREGS - callinc * 4;
243 put_user(regs->areg[caller_ars+1],
244 (unsigned __user*)(usp - 12));
245 }
246 childregs->wmask = 1;
247 childregs->windowstart = 1;
248 childregs->windowbase = 0;
Marc Gauthier84ed3052012-10-15 03:55:35 +0400249 } else {
250 int len = childregs->wmask & ~0xf;
251 memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
252 &regs->areg[XCHAL_NUM_AREGS - len/4], len);
253 }
Chris Zankelc658eac2008-02-12 13:17:07 -0800254// FIXME: we need to set THREADPTR in thread_info...
Chris Zankel5a0015d2005-06-23 22:01:16 -0700255 if (clone_flags & CLONE_SETTLS)
256 childregs->areg[2] = childregs->areg[6];
257
258 } else {
259 /* In kernel space, we start a new thread with a new stack. */
260 childregs->wmask = 1;
Marc Gauthier84ed3052012-10-15 03:55:35 +0400261 childregs->areg[1] = tos;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700262 }
Chris Zankelc658eac2008-02-12 13:17:07 -0800263
264#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
265 ti = task_thread_info(p);
266 ti->cpenable = 0;
267#endif
268
Chris Zankel5a0015d2005-06-23 22:01:16 -0700269 return 0;
270}
271
272
273/*
Chris Zankel5a0015d2005-06-23 22:01:16 -0700274 * These bracket the sleeping functions..
275 */
276
277unsigned long get_wchan(struct task_struct *p)
278{
279 unsigned long sp, pc;
Al Viro04fe6fa2006-01-12 01:05:50 -0800280 unsigned long stack_page = (unsigned long) task_stack_page(p);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700281 int count = 0;
282
283 if (!p || p == current || p->state == TASK_RUNNING)
284 return 0;
285
286 sp = p->thread.sp;
287 pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
288
289 do {
290 if (sp < stack_page + sizeof(struct task_struct) ||
291 sp >= (stack_page + THREAD_SIZE) ||
292 pc == 0)
293 return 0;
294 if (!in_sched_functions(pc))
295 return pc;
296
297 /* Stack layout: sp-4: ra, sp-3: sp' */
298
299 pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
300 sp = *(unsigned long *)sp - 3;
301 } while (count++ < 16);
302 return 0;
303}
304
305/*
Chris Zankel5a0015d2005-06-23 22:01:16 -0700306 * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
307 * of processor registers. Besides different ordering,
308 * xtensa_gregset_t contains non-live register information that
309 * 'struct pt_regs' does not. Exception handling (primarily) uses
310 * 'struct pt_regs'. Core files and ptrace use xtensa_gregset_t.
311 *
312 */
313
Chris Zankelc658eac2008-02-12 13:17:07 -0800314void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700315{
Chris Zankelc658eac2008-02-12 13:17:07 -0800316 unsigned long wb, ws, wm;
317 int live, last;
318
319 wb = regs->windowbase;
320 ws = regs->windowstart;
321 wm = regs->wmask;
322 ws = ((ws >> wb) | (ws << (WSBITS - wb))) & ((1 << WSBITS) - 1);
323
324 /* Don't leak any random bits. */
325
Alan Cox688bb412012-07-11 14:02:50 -0700326 memset(elfregs, 0, sizeof(*elfregs));
Chris Zankelc658eac2008-02-12 13:17:07 -0800327
Chris Zankel5a0015d2005-06-23 22:01:16 -0700328 /* Note: PS.EXCM is not set while user task is running; its
329 * being set in regs->ps is for exception handling convenience.
330 */
331
332 elfregs->pc = regs->pc;
Chris Zankel173d6682006-12-10 02:18:48 -0800333 elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT));
Chris Zankel5a0015d2005-06-23 22:01:16 -0700334 elfregs->lbeg = regs->lbeg;
335 elfregs->lend = regs->lend;
336 elfregs->lcount = regs->lcount;
337 elfregs->sar = regs->sar;
Chris Zankelc658eac2008-02-12 13:17:07 -0800338 elfregs->windowstart = ws;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700339
Chris Zankelc658eac2008-02-12 13:17:07 -0800340 live = (wm & 2) ? 4 : (wm & 4) ? 8 : (wm & 8) ? 12 : 16;
341 last = XCHAL_NUM_AREGS - (wm >> 4) * 4;
342 memcpy(elfregs->a, regs->areg, live * 4);
343 memcpy(elfregs->a + last, regs->areg + last, (wm >> 4) * 16);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700344}
345
Chris Zankelc658eac2008-02-12 13:17:07 -0800346int dump_fpu(void)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700347{
Chris Zankel5a0015d2005-06-23 22:01:16 -0700348 return 0;
349}
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800350
351asmlinkage
352long xtensa_clone(unsigned long clone_flags, unsigned long newsp,
353 void __user *parent_tid, void *child_tls,
354 void __user *child_tid, long a5,
355 struct pt_regs *regs)
356{
357 if (!newsp)
358 newsp = regs->areg[1];
359 return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
360}
361
362/*
Chris Zankelc658eac2008-02-12 13:17:07 -0800363 * xtensa_execve() executes a new program.
364 */
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800365
366asmlinkage
David Howellsd7627462010-08-17 23:52:56 +0100367long xtensa_execve(const char __user *name,
368 const char __user *const __user *argv,
369 const char __user *const __user *envp,
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800370 long a3, long a4, long a5,
371 struct pt_regs *regs)
372{
373 long error;
Jeff Layton91a27b22012-10-10 15:25:28 -0400374 struct filename *filename;
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800375
376 filename = getname(name);
377 error = PTR_ERR(filename);
378 if (IS_ERR(filename))
379 goto out;
Jeff Layton91a27b22012-10-10 15:25:28 -0400380 error = do_execve(filename->name, argv, envp, regs);
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800381 putname(filename);
382out:
383 return error;
384}
385